Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ export interface ComposerNumberFormatting<
| Key
| ResourceKeys
| NumberOptions<Key | ResourceKeys, Locales>
): string
): string | Intl.NumberFormatPart[]
/**
* Number Formatting
*
Expand All @@ -1229,7 +1229,7 @@ export interface ComposerNumberFormatting<
| ResourceKeys
| NumberOptions<Key | ResourceKeys, Locales>,
locale: Locales
): string
): string | Intl.NumberFormatPart[]
}

/**
Expand Down Expand Up @@ -2290,14 +2290,14 @@ export function createComposer(options: any = {}): any {
}

// n
function n(...args: unknown[]): string {
return wrapWithDeps<{}, string>(
context => Reflect.apply(number, null, [context, ...args]) as string,
function n(...args: unknown[]): string | Intl.NumberFormatPart[] {
return wrapWithDeps<{}, string | Intl.NumberFormatPart[]>(
context => Reflect.apply(number, null, [context, ...args]),
() => parseNumberArgs(...args),
'number format',
root => Reflect.apply(root.n, root, [...args]),
() => MISSING_RESOLVE_VALUE,
val => isString(val)
val => isString(val) || isArray(val)
)
}

Expand Down
14 changes: 7 additions & 7 deletions packages/vue-i18n-core/test/composer.test-d.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If n ($n) will pass the NumberOptions, n ($n) would be a good, inferred return type,
such as, pass string or Intl.NumberFormatPart[] to type parameter and the return type of it, or automatically infer from NumberOptions

$n(value: number, options: NumberOptions): string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am new to Typescript and have tried to add the change you requested. Please have a look.
Do let me know if I might have missed something. I'll work on that.
Thanks

Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,15 @@ test('strict composer with direct options', () => {
strictDirectComposer.d(new Date(), 'custom' as any)
).toEqualTypeOf<string>()
expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf<string>()
expectTypeOf(
strictDirectComposer.n(1, 'currency', 'zh')
).toEqualTypeOf<string>()
expectTypeOf(strictDirectComposer.n(1, 'currency', 'zh')).toEqualTypeOf<
string | Intl.NumberFormatPart[]
>()
expectTypeOf(
strictDirectComposer.n(1, { key: 'currency', locale: 'en' })
).toEqualTypeOf<string>()
expectTypeOf(
strictDirectComposer.n(1, 'custom' as any)
).toEqualTypeOf<string>()
).toEqualTypeOf<string | Intl.NumberFormatPart[]>()
expectTypeOf(strictDirectComposer.n(1, 'custom' as any)).toEqualTypeOf<
string | Intl.NumberFormatPart[]
>()

// const noOptionsComposer = createComposer({ missingWarn: true })
const noOptionsComposer = createComposer({ locale: 'en' })
Expand Down
77 changes: 77 additions & 0 deletions packages/vue-i18n-core/test/composer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,83 @@ describe('n', () => {
})
expect(n(0.99, { key: 'percent' })).toEqual('')
})

test('part formating with n', () => {
const { n } = createComposer({
locale: 'en-US',
fallbackLocale: ['ja-JP'],
numberFormats: {
'en-US': {
currency: {
style: 'currency',
currency: 'USD',
currencyDisplay: 'symbol'
},
decimal: {
style: 'decimal',
useGrouping: true
}
},
'ja-JP': {
currency: {
style: 'currency',
currency: 'JPY' /*, currencyDisplay: 'symbol'*/
},
numeric: {
style: 'decimal',
useGrouping: false
},
percent: {
style: 'percent',
useGrouping: true
}
}
}
})
expect(n(0.99, { key: 'currency', part: true })).toEqual([
{ type: 'currency', value: '$' },
{ type: 'integer', value: '0' },
{ type: 'decimal', value: '.' },
{ type: 'fraction', value: '99' }
])
expect(
n(10100, {
key: 'currency',
locale: 'ja-JP',
currency: 'EUR',
part: true
})
).toEqual([
{ type: 'currency', value: '€' },
{ type: 'integer', value: '10' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '100' },
{ type: 'decimal', value: '.' },
{ type: 'fraction', value: '00' }
])
// expect(n(12145281111, 'decimal', 'ja-JP')).toEqual([])
expect(n(12145281000, { key: 'percent', part: true })).toEqual([
{ type: 'integer', value: '1' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '214' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '528' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '100' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '000' },
{ type: 'percentSign', value: '%' }
])
expect(n(12145281111, { key: 'decimal', part: true })).toEqual([
{ type: 'integer', value: '12' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '145' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '281' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '111' }
])
})
})

describe('tm', () => {
Expand Down