@@ -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
5561export 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
157170export 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
181201export function clearDateTimeFormat (
0 commit comments