Skip to content

Commit e8c8062

Browse files
mauryapariParitosh Mauryakazupon
authored
feat: Part options support $d (#2177)
* Added support for part option in Added test cases for the same * Updated function defination for * Added support for part option in dates formatting * Update packages/vue-i18n-core/test/composer.test.ts --------- Co-authored-by: Paritosh Maurya <[email protected]> Co-authored-by: kazuya kawaguchi <[email protected]>
1 parent 122c9ca commit e8c8062

File tree

4 files changed

+118
-13
lines changed

4 files changed

+118
-13
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,13 +1111,19 @@ export interface ComposerDateTimeFormatting<
11111111
*
11121112
* @returns Formatted value
11131113
*/
1114-
<Value extends number | Date | string = number, Key extends string = string>(
1114+
<
1115+
Value extends number | Date | string = number,
1116+
Key extends string = string,
1117+
Return extends string | Intl.DateTimeFormatPart[] =
1118+
| string
1119+
| Intl.DateTimeFormatPart[]
1120+
>(
11151121
value: Value,
11161122
keyOrOptions:
11171123
| Key
11181124
| ResourceKeys
11191125
| DateTimeOptions<Key | ResourceKeys, Locales>
1120-
): string
1126+
): Return
11211127
/**
11221128
* Datetime formatting
11231129
*
@@ -1132,14 +1138,20 @@ export interface ComposerDateTimeFormatting<
11321138
*
11331139
* @returns Formatted value
11341140
*/
1135-
<Value extends number | Date | string = number, Key extends string = string>(
1141+
<
1142+
Value extends number | Date | string = number,
1143+
Key extends string = string,
1144+
Return extends string | Intl.DateTimeFormatPart[] =
1145+
| string
1146+
| Intl.DateTimeFormatPart[]
1147+
>(
11361148
value: Value,
11371149
keyOrOptions:
11381150
| Key
11391151
| ResourceKeys
11401152
| DateTimeOptions<Key | ResourceKeys, Locales>,
11411153
locale: Locales
1142-
): string
1154+
): Return
11431155
}
11441156

11451157
/**
@@ -2288,14 +2300,17 @@ export function createComposer(options: any = {}): any {
22882300
}
22892301

22902302
// d
2291-
function d(...args: unknown[]): string {
2292-
return wrapWithDeps<{}, string>(
2293-
context => Reflect.apply(datetime, null, [context, ...args]) as string,
2303+
function d(...args: unknown[]): string | Intl.DateTimeFormatPart[] {
2304+
return wrapWithDeps<{}, string | Intl.DateTimeFormatPart[]>(
2305+
context =>
2306+
Reflect.apply(datetime, null, [context, ...args]) as
2307+
| string
2308+
| Intl.DateTimeFormatPart[],
22942309
() => parseDateTimeArgs(...args),
22952310
'datetime format',
22962311
root => Reflect.apply(root.d, root, [...args]),
22972312
() => MISSING_RESOLVE_VALUE,
2298-
val => isString(val)
2313+
val => isString(val) || isArray(val)
22992314
)
23002315
}
23012316

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
@@ -1058,6 +1058,83 @@ describe('d', () => {
10581058
'12/20/2012, 07:00 AM'
10591059
)
10601060
})
1061+
1062+
test('parts formatting', () => {
1063+
const { d } = createComposer({
1064+
locale: 'en-US',
1065+
datetimeFormats: {
1066+
'en-US': {
1067+
short: {
1068+
year: 'numeric',
1069+
month: '2-digit',
1070+
day: '2-digit',
1071+
hour: '2-digit',
1072+
minute: '2-digit',
1073+
timeZone: 'America/New_York'
1074+
}
1075+
},
1076+
'ja-JP': {
1077+
long: {
1078+
year: 'numeric',
1079+
month: '2-digit',
1080+
day: '2-digit',
1081+
hour: '2-digit',
1082+
minute: '2-digit',
1083+
second: '2-digit',
1084+
timeZone: 'America/New_York'
1085+
},
1086+
short: {
1087+
year: 'numeric',
1088+
month: '2-digit',
1089+
day: '2-digit',
1090+
hour: '2-digit',
1091+
minute: '2-digit',
1092+
timeZone: 'Asia/Tokyo'
1093+
}
1094+
}
1095+
}
1096+
})
1097+
const dt = new Date(2015, 12)
1098+
expect(d(dt, { key: 'short', part: true, year: '2-digit' })).toEqual([
1099+
{ type: 'month', value: '12' },
1100+
{ type: 'literal', value: '/' },
1101+
{ type: 'day', value: '31' },
1102+
{ type: 'literal', value: '/' },
1103+
{ type: 'year', value: '15' },
1104+
{ type: 'literal', value: ', ' },
1105+
{ type: 'hour', value: '07' },
1106+
{ type: 'literal', value: ':' },
1107+
{ type: 'minute', value: '00' },
1108+
{ type: 'literal', value: ' ' },
1109+
{ type: 'dayPeriod', value: 'PM' }
1110+
])
1111+
expect(d(dt, { key: 'short', locale: 'en-US', part: true })).toEqual([
1112+
{ type: 'month', value: '12' },
1113+
{ type: 'literal', value: '/' },
1114+
{ type: 'day', value: '31' },
1115+
{ type: 'literal', value: '/' },
1116+
{ type: 'year', value: '2015' },
1117+
{ type: 'literal', value: ', ' },
1118+
{ type: 'hour', value: '07' },
1119+
{ type: 'literal', value: ':' },
1120+
{ type: 'minute', value: '00' },
1121+
{ type: 'literal', value: ' ' },
1122+
{ type: 'dayPeriod', value: 'PM' }
1123+
])
1124+
expect(d(dt, { key: 'long', locale: 'ja-JP', part: true })).toEqual([
1125+
{ type: 'year', value: '2015' },
1126+
{ type: 'literal', value: '/' },
1127+
{ type: 'month', value: '12' },
1128+
{ type: 'literal', value: '/' },
1129+
{ type: 'day', value: '31' },
1130+
{ type: 'literal', value: ' ' },
1131+
{ type: 'hour', value: '19' },
1132+
{ type: 'literal', value: ':' },
1133+
{ type: 'minute', value: '00' },
1134+
{ type: 'literal', value: ':' },
1135+
{ type: 'second', value: '00' }
1136+
])
1137+
})
10611138
})
10621139

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

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,11 @@ declare module 'vue' {
600600
* @returns formatted value
601601
*/
602602
$d<
603-
Value extends number | Date | string = number,
603+
Value extends number | Date = number,
604604
Key extends string = string,
605+
Return extends string | Intl.DateTimeFormatPart[] =
606+
| string
607+
| Intl.DateTimeFormatPart[],
605608
DefinedDateTimeFormat extends
606609
RemovedIndexResources<DefineDateTimeFormat> = RemovedIndexResources<DefineDateTimeFormat>,
607610
Keys = IsEmptyObject<DefinedDateTimeFormat> extends false
@@ -614,7 +617,7 @@ declare module 'vue' {
614617
value: Value,
615618
options: DateTimeOptions<Key | ResourceKeys>,
616619
locale: Locale
617-
): string
620+
): Return
618621
/**
619622
* Number formatting
620623
*

0 commit comments

Comments
 (0)