Skip to content

Commit b890701

Browse files
authored
part option support for d (#2180)
1 parent bc92dc4 commit b890701

File tree

4 files changed

+131
-12
lines changed

4 files changed

+131
-12
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,13 +1113,19 @@ export interface ComposerDateTimeFormatting<
11131113
*
11141114
* @returns Formatted value
11151115
*/
1116-
<Value extends number | Date | string = number, Key extends string = string>(
1116+
<
1117+
Value extends number | Date | string = number,
1118+
Key extends string = string,
1119+
Return extends string | Intl.DateTimeFormatPart[] =
1120+
| string
1121+
| Intl.DateTimeFormatPart[]
1122+
>(
11171123
value: Value,
11181124
keyOrOptions:
11191125
| Key
11201126
| ResourceKeys
11211127
| DateTimeOptions<Key | ResourceKeys, Locales>
1122-
): string
1128+
): Return
11231129
/**
11241130
* Datetime formatting
11251131
*
@@ -1134,14 +1140,20 @@ export interface ComposerDateTimeFormatting<
11341140
*
11351141
* @returns Formatted value
11361142
*/
1137-
<Value extends number | Date | string = number, Key extends string = string>(
1143+
<
1144+
Value extends number | Date | string = number,
1145+
Key extends string = string,
1146+
Return extends string | Intl.DateTimeFormatPart[] =
1147+
| string
1148+
| Intl.DateTimeFormatPart[]
1149+
>(
11381150
value: Value,
11391151
keyOrOptions:
11401152
| Key
11411153
| ResourceKeys
11421154
| DateTimeOptions<Key | ResourceKeys, Locales>,
11431155
locale: Locales
1144-
): string
1156+
): Return
11451157
}
11461158

11471159
/**
@@ -2285,14 +2297,17 @@ export function createComposer(options: any = {}): any {
22852297
}
22862298

22872299
// d
2288-
function d(...args: unknown[]): string {
2289-
return wrapWithDeps<{}, string>(
2290-
context => Reflect.apply(datetime, null, [context, ...args]) as string,
2300+
function d(...args: unknown[]): string | Intl.DateTimeFormatPart[] {
2301+
return wrapWithDeps<{}, string | Intl.DateTimeFormatPart[]>(
2302+
context =>
2303+
Reflect.apply(datetime, null, [context, ...args]) as
2304+
| string
2305+
| Intl.DateTimeFormatPart[],
22912306
() => parseDateTimeArgs(...args),
22922307
'datetime format',
22932308
root => Reflect.apply(root.d, root, [...args]),
22942309
() => MISSING_RESOLVE_VALUE,
2295-
val => isString(val)
2310+
val => isString(val) || isArray(val)
22962311
)
22972312
}
22982313

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,23 @@ test('strict composer with direct options', () => {
344344
}>()
345345
expectTypeOf(strictDirectComposer.d(new Date())).toEqualTypeOf<string>()
346346
expectTypeOf(
347-
strictDirectComposer.d(new Date(), 'short', 'ja-JP')
347+
strictDirectComposer.d<Date, string, string>(new Date(), 'short', 'ja-JP')
348348
).toEqualTypeOf<string>()
349349
expectTypeOf(
350350
strictDirectComposer.d(new Date(), { key: 'short', locale: 'zh' })
351-
).toEqualTypeOf<string>()
351+
).toEqualTypeOf<string | Intl.DateTimeFormatPart[]>()
352+
expectTypeOf(
353+
strictDirectComposer.d<Date, string, Intl.DateTimeFormatPart[]>(
354+
new Date(),
355+
{
356+
key: 'short',
357+
locale: 'zh',
358+
part: true
359+
}
360+
)
361+
).toEqualTypeOf<Intl.DateTimeFormatPart[]>()
352362
expectTypeOf(
353-
strictDirectComposer.d(new Date(), 'custom' as any)
363+
strictDirectComposer.d<Date, string, string>(new Date(), 'custom' as any)
354364
).toEqualTypeOf<string>()
355365
expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf<string>()
356366
expectTypeOf(strictDirectComposer.n(1, 'currency', 'zh')).toEqualTypeOf<

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,83 @@ describe('d', () => {
10471047
'12/20/2012, 07:00 AM'
10481048
)
10491049
})
1050+
1051+
test('parts formatting', () => {
1052+
const { d } = createComposer({
1053+
locale: 'en-US',
1054+
datetimeFormats: {
1055+
'en-US': {
1056+
short: {
1057+
year: 'numeric',
1058+
month: '2-digit',
1059+
day: '2-digit',
1060+
hour: '2-digit',
1061+
minute: '2-digit',
1062+
timeZone: 'America/New_York'
1063+
}
1064+
},
1065+
'ja-JP': {
1066+
long: {
1067+
year: 'numeric',
1068+
month: '2-digit',
1069+
day: '2-digit',
1070+
hour: '2-digit',
1071+
minute: '2-digit',
1072+
second: '2-digit',
1073+
timeZone: 'America/New_York'
1074+
},
1075+
short: {
1076+
year: 'numeric',
1077+
month: '2-digit',
1078+
day: '2-digit',
1079+
hour: '2-digit',
1080+
minute: '2-digit',
1081+
timeZone: 'Asia/Tokyo'
1082+
}
1083+
}
1084+
}
1085+
})
1086+
const dt = new Date(2015, 12)
1087+
expect(d(dt, { key: 'short', part: true, year: '2-digit' })).toEqual([
1088+
{ type: 'month', value: '12' },
1089+
{ type: 'literal', value: '/' },
1090+
{ type: 'day', value: '31' },
1091+
{ type: 'literal', value: '/' },
1092+
{ type: 'year', value: '15' },
1093+
{ type: 'literal', value: ', ' },
1094+
{ type: 'hour', value: '07' },
1095+
{ type: 'literal', value: ':' },
1096+
{ type: 'minute', value: '00' },
1097+
{ type: 'literal', value: ' ' },
1098+
{ type: 'dayPeriod', value: 'PM' }
1099+
])
1100+
expect(d(dt, { key: 'short', locale: 'en-US', part: true })).toEqual([
1101+
{ type: 'month', value: '12' },
1102+
{ type: 'literal', value: '/' },
1103+
{ type: 'day', value: '31' },
1104+
{ type: 'literal', value: '/' },
1105+
{ type: 'year', value: '2015' },
1106+
{ type: 'literal', value: ', ' },
1107+
{ type: 'hour', value: '07' },
1108+
{ type: 'literal', value: ':' },
1109+
{ type: 'minute', value: '00' },
1110+
{ type: 'literal', value: ' ' },
1111+
{ type: 'dayPeriod', value: 'PM' }
1112+
])
1113+
expect(d(dt, { key: 'long', locale: 'ja-JP', part: true })).toEqual([
1114+
{ type: 'year', value: '2015' },
1115+
{ type: 'literal', value: '/' },
1116+
{ type: 'month', value: '12' },
1117+
{ type: 'literal', value: '/' },
1118+
{ type: 'day', value: '31' },
1119+
{ type: 'literal', value: ' ' },
1120+
{ type: 'hour', value: '19' },
1121+
{ type: 'literal', value: ':' },
1122+
{ type: 'minute', value: '00' },
1123+
{ type: 'literal', value: ':' },
1124+
{ type: 'second', value: '00' }
1125+
])
1126+
})
10501127
})
10511128

10521129
describe('n', () => {

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,24 @@ declare module 'vue' {
671671
*
672672
* @returns formatted value
673673
*/
674-
$d(value: number | Date, options: DateTimeOptions): string
674+
$d<
675+
value extends number | Date = number,
676+
Key extends string = string,
677+
Return extends string | Intl.DateTimeFormatPart[] =
678+
| string
679+
| Intl.DateTimeFormatPart[],
680+
DefinedDateTimeFormat extends
681+
RemovedIndexResources<DefineDateTimeFormat> = RemovedIndexResources<DefineDateTimeFormat>,
682+
Keys = IsEmptyObject<DefinedDateTimeFormat> extends false
683+
? PickupFormatPathKeys<{
684+
[K in keyof DefinedDateTimeFormat]: DefinedDateTimeFormat[K]
685+
}>
686+
: never,
687+
ResourceKeys extends Keys = IsNever<Keys> extends false ? Keys : never
688+
>(
689+
value: number | Date,
690+
options: DateTimeOptions<Key, ResourceKeys>
691+
): Return
675692
/**
676693
* Number formatting
677694
*

0 commit comments

Comments
 (0)