Skip to content

Commit db0588e

Browse files
committed
feat: override datetime format options with function options
1 parent 541b2f1 commit db0588e

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/runtime/datetime.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
isBoolean,
1515
isPlainObject,
1616
isDate,
17-
isNumber
17+
isNumber,
18+
isEmptyObject
1819
} from '../utils'
1920

2021
/**
@@ -50,6 +51,11 @@ import {
5051
*
5152
* // if you specify `part` options, you can get an array of objects containing the formatted datetime in parts
5253
* datetime(context, value, { key: 'short', part: true })
54+
*
55+
* // orverride context.datetimeFormats[locale] options with functino options
56+
* datetime(cnotext, value, 'short', { currency: 'EUR' })
57+
* datetime(cnotext, value, 'short', 'ja-JP', { currency: 'EUR' })
58+
* datetime(context, value, { key: 'short', part: true }, { currency: 'EUR'})
5359
*/
5460

5561
export type DateTimeOptions = {
@@ -103,7 +109,7 @@ export function datetime(
103109
return MISSING_RESOLVE_VALUE
104110
}
105111

106-
const [value, options] = parseDateTimeArgs(...args)
112+
const [value, options, orverrides] = parseDateTimeArgs(...args)
107113
const { key } = options
108114
const missingWarn = isBoolean(options.missingWarn)
109115
? options.missingWarn
@@ -145,20 +151,28 @@ export function datetime(
145151
return unresolving ? NOT_REOSLVED : key
146152
}
147153

148-
const id = `${targetLocale}__${key}`
154+
let id = `${targetLocale}__${key}`
155+
if (!isEmptyObject(orverrides)) {
156+
id = `${id}__${JSON.stringify(orverrides)}`
157+
}
158+
149159
let formatter = _datetimeFormatters.get(id)
150160
if (!formatter) {
151-
formatter = new Intl.DateTimeFormat(targetLocale, format)
161+
formatter = new Intl.DateTimeFormat(
162+
targetLocale,
163+
Object.assign({}, format, orverrides)
164+
)
152165
_datetimeFormatters.set(id, formatter)
153166
}
154167
return !part ? formatter.format(value) : formatter.formatToParts(value)
155168
}
156169

157170
export function parseDateTimeArgs(
158171
...args: unknown[]
159-
): [number | Date, DateTimeOptions] {
160-
const [arg1, arg2, arg3] = args
172+
): [number | Date, DateTimeOptions, Intl.DateTimeFormatOptions] {
173+
const [arg1, arg2, arg3, arg4] = args
161174
let options = {} as DateTimeOptions
175+
let orverrides = {} as Intl.DateTimeFormatOptions
162176

163177
if (!(isNumber(arg1) || isDate(arg1))) {
164178
throw new Error('TODO')
@@ -173,9 +187,15 @@ export function parseDateTimeArgs(
173187

174188
if (isString(arg3)) {
175189
options.locale = arg3
190+
} else if (isPlainObject(arg3)) {
191+
orverrides = arg3
192+
}
193+
194+
if (isPlainObject(arg4)) {
195+
orverrides = arg4
176196
}
177197

178-
return [value, options]
198+
return [value, options, orverrides]
179199
}
180200

181201
export function clearDateTimeFormat(

test/runtime/datetime.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ test('with object argument', () => {
132132
)
133133
})
134134

135+
test('override format options with number function options', () => {
136+
const mockAvailabilities = Availabilities as jest.Mocked<
137+
typeof Availabilities
138+
>
139+
mockAvailabilities.numberFormat = true
140+
141+
const ctx = context({
142+
locale: 'en-US',
143+
fallbackLocale: ['ja-JP'],
144+
datetimeFormats
145+
})
146+
147+
expect(datetime(ctx, dt, 'short', { year: '2-digit' })).toEqual(
148+
'12/19/12, 10:00 PM'
149+
)
150+
expect(datetime(ctx, dt, 'short', 'ja-JP', { year: '2-digit' })).toEqual(
151+
'12/12/20 12:00'
152+
)
153+
expect(
154+
datetime(ctx, dt, { key: 'short', locale: 'ja-JP' }, { year: '2-digit' })
155+
).toEqual('12/12/20 12:00')
156+
})
157+
135158
test('fallback', () => {
136159
const mockWarn = warn as jest.MockedFunction<typeof warn>
137160
mockWarn.mockImplementation(() => {}) // eslint-disable-line @typescript-eslint/no-empty-function

0 commit comments

Comments
 (0)