Skip to content

Commit 07143af

Browse files
mauryapariParitosh MauryakazuponCopilot
authored
feat: Part options support for $n (#2170)
* Added support for part option in Added test cases for the same * Update packages/vue-i18n-core/test/composer.test.ts Co-authored-by: Copilot <[email protected]> * Updated function defination for * Updated function defination for --------- Co-authored-by: Paritosh Maurya <[email protected]> Co-authored-by: kazuya kawaguchi <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 2e04bed commit 07143af

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

packages/vue-i18n-core/src/composer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ export interface ComposerNumberFormatting<
12071207
| Key
12081208
| ResourceKeys
12091209
| NumberOptions<Key | ResourceKeys, Locales>
1210-
): string
1210+
): string | Intl.NumberFormatPart[]
12111211
/**
12121212
* Number Formatting
12131213
*
@@ -1229,7 +1229,7 @@ export interface ComposerNumberFormatting<
12291229
| ResourceKeys
12301230
| NumberOptions<Key | ResourceKeys, Locales>,
12311231
locale: Locales
1232-
): string
1232+
): string | Intl.NumberFormatPart[]
12331233
}
12341234

12351235
/**
@@ -2290,14 +2290,14 @@ export function createComposer(options: any = {}): any {
22902290
}
22912291

22922292
// n
2293-
function n(...args: unknown[]): string {
2294-
return wrapWithDeps<{}, string>(
2295-
context => Reflect.apply(number, null, [context, ...args]) as string,
2293+
function n(...args: unknown[]): string | Intl.NumberFormatPart[] {
2294+
return wrapWithDeps<{}, string | Intl.NumberFormatPart[]>(
2295+
context => Reflect.apply(number, null, [context, ...args]),
22962296
() => parseNumberArgs(...args),
22972297
'number format',
22982298
root => Reflect.apply(root.n, root, [...args]),
22992299
() => MISSING_RESOLVE_VALUE,
2300-
val => isString(val)
2300+
val => isString(val) || isArray(val)
23012301
)
23022302
}
23032303

packages/vue-i18n-core/test/composer.test-d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,15 @@ test('strict composer with direct options', () => {
353353
strictDirectComposer.d(new Date(), 'custom' as any)
354354
).toEqualTypeOf<string>()
355355
expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf<string>()
356-
expectTypeOf(
357-
strictDirectComposer.n(1, 'currency', 'zh')
358-
).toEqualTypeOf<string>()
356+
expectTypeOf(strictDirectComposer.n(1, 'currency', 'zh')).toEqualTypeOf<
357+
string | Intl.NumberFormatPart[]
358+
>()
359359
expectTypeOf(
360360
strictDirectComposer.n(1, { key: 'currency', locale: 'en' })
361-
).toEqualTypeOf<string>()
362-
expectTypeOf(
363-
strictDirectComposer.n(1, 'custom' as any)
364-
).toEqualTypeOf<string>()
361+
).toEqualTypeOf<string | Intl.NumberFormatPart[]>()
362+
expectTypeOf(strictDirectComposer.n(1, 'custom' as any)).toEqualTypeOf<
363+
string | Intl.NumberFormatPart[]
364+
>()
365365

366366
// const noOptionsComposer = createComposer({ missingWarn: true })
367367
const noOptionsComposer = createComposer({ locale: 'en' })

packages/vue-i18n-core/test/composer.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,83 @@ describe('n', () => {
12131213
})
12141214
expect(n(0.99, { key: 'percent' })).toEqual('')
12151215
})
1216+
1217+
test('part formatting with n', () => {
1218+
const { n } = createComposer({
1219+
locale: 'en-US',
1220+
fallbackLocale: ['ja-JP'],
1221+
numberFormats: {
1222+
'en-US': {
1223+
currency: {
1224+
style: 'currency',
1225+
currency: 'USD',
1226+
currencyDisplay: 'symbol'
1227+
},
1228+
decimal: {
1229+
style: 'decimal',
1230+
useGrouping: true
1231+
}
1232+
},
1233+
'ja-JP': {
1234+
currency: {
1235+
style: 'currency',
1236+
currency: 'JPY' /*, currencyDisplay: 'symbol'*/
1237+
},
1238+
numeric: {
1239+
style: 'decimal',
1240+
useGrouping: false
1241+
},
1242+
percent: {
1243+
style: 'percent',
1244+
useGrouping: true
1245+
}
1246+
}
1247+
}
1248+
})
1249+
expect(n(0.99, { key: 'currency', part: true })).toEqual([
1250+
{ type: 'currency', value: '$' },
1251+
{ type: 'integer', value: '0' },
1252+
{ type: 'decimal', value: '.' },
1253+
{ type: 'fraction', value: '99' }
1254+
])
1255+
expect(
1256+
n(10100, {
1257+
key: 'currency',
1258+
locale: 'ja-JP',
1259+
currency: 'EUR',
1260+
part: true
1261+
})
1262+
).toEqual([
1263+
{ type: 'currency', value: '€' },
1264+
{ type: 'integer', value: '10' },
1265+
{ type: 'group', value: ',' },
1266+
{ type: 'integer', value: '100' },
1267+
{ type: 'decimal', value: '.' },
1268+
{ type: 'fraction', value: '00' }
1269+
])
1270+
// expect(n(12145281111, 'decimal', 'ja-JP')).toEqual([])
1271+
expect(n(12145281000, { key: 'percent', part: true })).toEqual([
1272+
{ type: 'integer', value: '1' },
1273+
{ type: 'group', value: ',' },
1274+
{ type: 'integer', value: '214' },
1275+
{ type: 'group', value: ',' },
1276+
{ type: 'integer', value: '528' },
1277+
{ type: 'group', value: ',' },
1278+
{ type: 'integer', value: '100' },
1279+
{ type: 'group', value: ',' },
1280+
{ type: 'integer', value: '000' },
1281+
{ type: 'percentSign', value: '%' }
1282+
])
1283+
expect(n(12145281111, { key: 'decimal', part: true })).toEqual([
1284+
{ type: 'integer', value: '12' },
1285+
{ type: 'group', value: ',' },
1286+
{ type: 'integer', value: '145' },
1287+
{ type: 'group', value: ',' },
1288+
{ type: 'integer', value: '281' },
1289+
{ type: 'group', value: ',' },
1290+
{ type: 'integer', value: '111' }
1291+
])
1292+
})
12161293
})
12171294

12181295
describe('tm', () => {

packages/vue-i18n/src/vue.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,10 @@ declare module 'vue' {
846846
*
847847
* @returns formatted value
848848
*/
849-
$n(value: number, options: NumberOptions): string
849+
$n<OptionsType extends NumberOptions>(
850+
value: number,
851+
options: OptionsType
852+
): OptionsType['part'] extends true ? Intl.NumberFormatPart[] : string
850853
/**
851854
* Locale messages getter
852855
*

0 commit comments

Comments
 (0)