Skip to content

Commit 886247c

Browse files
committed
refactoring
1 parent 3ee3bd5 commit 886247c

File tree

3 files changed

+91
-111
lines changed

3 files changed

+91
-111
lines changed

src/composer.ts

Lines changed: 83 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -392,64 +392,68 @@ export function createComposer(options: ComposerOptions = {}): Composer {
392392
_context.missing = _runtimeMissing
393393
}
394394

395-
// t
396-
const t = (...args: unknown[]): string => {
397-
return computed<string>((): string => {
398-
const ret = translate(_context, ...args)
399-
if (isNumber(ret) && ret === NOT_REOSLVED) {
400-
const [key] = parseTranslateArgs(...args)
401-
if (__DEV__ && _fallbackRoot && __root) {
402-
warn(`Fall back to translate '${key}' with root locale.`)
395+
const defineComputed = <T>(
396+
fn: (context: RuntimeContext) => unknown,
397+
argumentParser: () => string,
398+
warnType: string,
399+
fallbackSuccess: (root: Composer) => T,
400+
fallbackFail: (key: string) => T,
401+
successCondition: (val: unknown) => boolean
402+
): ComputedRef<T> => {
403+
return computed<T>(
404+
(): T => {
405+
const ret = fn(_context)
406+
if (isNumber(ret) && ret === NOT_REOSLVED) {
407+
const key = argumentParser()
408+
if (__DEV__ && _fallbackRoot && __root) {
409+
warn(`Fall back to ${warnType} '${key}' with root locale.`)
410+
}
411+
return _fallbackRoot && __root
412+
? fallbackSuccess(__root)
413+
: fallbackFail(key)
414+
} else if (successCondition(ret)) {
415+
return ret as T
416+
} else {
417+
throw new Error('TODO:') // TODO
403418
}
404-
return _fallbackRoot && __root ? __root.t(...args) : key
405-
} else if (isString(ret)) {
406-
return ret
407-
} else {
408-
throw new Error('TODO:') // TODO
409419
}
410-
}).value
420+
)
421+
}
422+
423+
// t
424+
const t = (...args: unknown[]): string => {
425+
return defineComputed<string>(
426+
context => translate(context, ...args),
427+
() => parseTranslateArgs(...args)[0],
428+
'translate',
429+
root => root.t(...args),
430+
key => key,
431+
val => isString(val)
432+
).value
411433
}
412434

413435
// d
414436
const d = (...args: unknown[]): string => {
415-
return computed<string>((): string => {
416-
const ret = datetime(_context, ...args)
417-
if (isNumber(ret) && ret === NOT_REOSLVED) {
418-
const [, options] = parseDateTimeArgs(...args)
419-
if (__DEV__ && _fallbackRoot && __root) {
420-
const key = isString(options.key) ? options.key : ''
421-
warn(`Fall back to datetime format '${key}' with root locale.`)
422-
}
423-
return _fallbackRoot && __root
424-
? __root.d(...args)
425-
: MISSING_RESOLVE_VALUE
426-
} else if (isString(ret)) {
427-
return ret
428-
} else {
429-
throw new Error('TODO:') // TODO
430-
}
431-
}).value
437+
return defineComputed<string>(
438+
context => datetime(context, ...args),
439+
() => parseDateTimeArgs(...args)[0],
440+
'datetime format',
441+
root => root.d(...args),
442+
() => MISSING_RESOLVE_VALUE,
443+
val => isString(val)
444+
).value
432445
}
433446

434447
// n
435448
const n = (...args: unknown[]): string => {
436-
return computed<string>((): string => {
437-
const ret = number(_context, ...args)
438-
if (isNumber(ret) && ret === NOT_REOSLVED) {
439-
const [, options] = parseNumberArgs(...args)
440-
if (__DEV__ && _fallbackRoot && __root) {
441-
const key = isString(options.key) ? options.key : ''
442-
warn(`Fall back to number format '${key}' with root locale.`)
443-
}
444-
return _fallbackRoot && __root
445-
? __root.d(...args)
446-
: MISSING_RESOLVE_VALUE
447-
} else if (isString(ret)) {
448-
return ret
449-
} else {
450-
throw new Error('TODO:') // TODO
451-
}
452-
}).value
449+
return defineComputed<string>(
450+
context => number(context, ...args),
451+
() => parseNumberArgs(...args)[0],
452+
'number format',
453+
root => root.n(...args),
454+
() => MISSING_RESOLVE_VALUE,
455+
val => isString(val)
456+
).value
453457
}
454458

455459
// for custom processor
@@ -465,73 +469,51 @@ export function createComposer(options: ComposerOptions = {}): Composer {
465469

466470
// __transrateVNode, using for `i18n-t` component
467471
const __transrateVNode = (...args: unknown[]): unknown => {
468-
return computed<unknown>((): unknown => {
469-
let ret: unknown
470-
try {
471-
// translate with custom processor
472-
_context.processor = processor
473-
ret = translate(_context, ...args)
474-
} finally {
475-
_context.processor = null
476-
}
477-
if (isNumber(ret) && ret === NOT_REOSLVED) {
478-
const [key] = parseTranslateArgs(...args)
479-
if (__DEV__ && _fallbackRoot && __root) {
480-
warn(`Fall back to translate '${key}' with root locale.`)
472+
return defineComputed<unknown>(
473+
context => {
474+
let ret: unknown
475+
try {
476+
context.processor = processor
477+
ret = translate(context, ...args)
478+
} finally {
479+
context.processor = null
481480
}
482-
return _fallbackRoot && __root ? __root.__transrateVNode(...args) : key
483-
} else if (isArray(ret)) {
484481
return ret
485-
} else {
486-
throw new Error('TODO:') // TODO
487-
}
488-
}).value
482+
},
483+
() => parseTranslateArgs(...args)[0],
484+
'translate',
485+
root => root.__transrateVNode(...args),
486+
key => key,
487+
val => isArray(val)
488+
).value
489489
}
490490

491491
// __numberParts, using for `i18n-n` component
492492
const __numberParts = (
493493
...args: unknown[]
494494
): string | Intl.NumberFormatPart[] => {
495-
return computed<string | Intl.NumberFormatPart[]>(():
496-
| string
497-
| Intl.NumberFormatPart[] => {
498-
const ret = number(_context, ...args)
499-
if (isNumber(ret) && ret === NOT_REOSLVED) {
500-
const [, options] = parseNumberArgs(...args)
501-
if (__DEV__ && _fallbackRoot && __root) {
502-
const key = isString(options.key) ? options.key : ''
503-
warn(`Fall back to number format '${key}' with root locale.`)
504-
}
505-
return _fallbackRoot && __root ? __root.__numberParts(...args) : []
506-
} else if (isString(ret) || isArray(ret)) {
507-
return ret
508-
} else {
509-
throw new Error('TODO:') // TODO
510-
}
511-
}).value
495+
return defineComputed<string | Intl.NumberFormatPart[]>(
496+
context => number(context, ...args),
497+
() => parseNumberArgs(...args)[0],
498+
'number format',
499+
root => root.__numberParts(...args),
500+
() => [],
501+
val => isString(val) || isArray(val)
502+
).value
512503
}
513504

514505
// __datetimeParts, using for `i18n-d` component
515506
const __datetimeParts = (
516507
...args: unknown[]
517508
): string | Intl.DateTimeFormatPart[] => {
518-
return computed<string | Intl.DateTimeFormatPart[]>(():
519-
| string
520-
| Intl.DateTimeFormatPart[] => {
521-
const ret = datetime(_context, ...args)
522-
if (isNumber(ret) && ret === NOT_REOSLVED) {
523-
const [, options] = parseDateTimeArgs(...args)
524-
if (__DEV__ && _fallbackRoot && __root) {
525-
const key = isString(options.key) ? options.key : ''
526-
warn(`Fall back to datetime format '${key}' with root locale.`)
527-
}
528-
return _fallbackRoot && __root ? __root.__datetimeParts(...args) : []
529-
} else if (isString(ret) || isArray(ret)) {
530-
return ret
531-
} else {
532-
throw new Error('TODO:') // TODO
533-
}
534-
}).value
509+
return defineComputed<string | Intl.DateTimeFormatPart[]>(
510+
context => datetime(context, ...args),
511+
() => parseDateTimeArgs(...args)[0],
512+
'datetime format',
513+
root => root.__datetimeParts(...args),
514+
() => [],
515+
val => isString(val) || isArray(val)
516+
).value
535517
}
536518

537519
// getLocaleMessage

src/runtime/datetime.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ export function datetime(
109109
return MISSING_RESOLVE_VALUE
110110
}
111111

112-
const [value, options, orverrides] = parseDateTimeArgs(...args)
113-
const { key } = options
112+
const [key, value, options, orverrides] = parseDateTimeArgs(...args)
114113
const missingWarn = isBoolean(options.missingWarn)
115114
? options.missingWarn
116115
: context.missingWarn
@@ -121,7 +120,7 @@ export function datetime(
121120
const locale = isString(options.locale) ? options.locale : context.locale
122121
const locales = getLocaleChain(context, fallbackLocale, locale)
123122

124-
if (!isString(key)) {
123+
if (!isString(key) || key === '') {
125124
return new Intl.DateTimeFormat(locale).format(value)
126125
}
127126

@@ -169,7 +168,7 @@ export function datetime(
169168

170169
export function parseDateTimeArgs(
171170
...args: unknown[]
172-
): [number | Date, DateTimeOptions, Intl.DateTimeFormatOptions] {
171+
): [string, number | Date, DateTimeOptions, Intl.DateTimeFormatOptions] {
173172
const [arg1, arg2, arg3, arg4] = args
174173
let options = {} as DateTimeOptions
175174
let orverrides = {} as Intl.DateTimeFormatOptions
@@ -195,7 +194,7 @@ export function parseDateTimeArgs(
195194
orverrides = arg4
196195
}
197196

198-
return [value, options, orverrides]
197+
return [options.key || '', value, options, orverrides]
199198
}
200199

201200
export function clearDateTimeFormat(

src/runtime/number.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ export function number(
107107
return MISSING_RESOLVE_VALUE
108108
}
109109

110-
const [value, options, orverrides] = parseNumberArgs(...args)
111-
const { key } = options
110+
const [key, value, options, orverrides] = parseNumberArgs(...args)
112111
const missingWarn = isBoolean(options.missingWarn)
113112
? options.missingWarn
114113
: context.missingWarn
@@ -119,7 +118,7 @@ export function number(
119118
const locale = isString(options.locale) ? options.locale : context.locale
120119
const locales = getLocaleChain(context, fallbackLocale, locale)
121120

122-
if (!isString(key)) {
121+
if (!isString(key) || key === '') {
123122
return new Intl.NumberFormat(locale).format(value)
124123
}
125124

@@ -167,7 +166,7 @@ export function number(
167166

168167
export function parseNumberArgs(
169168
...args: unknown[]
170-
): [number, NumberOptions, Intl.NumberFormatOptions] {
169+
): [string, number, NumberOptions, Intl.NumberFormatOptions] {
171170
const [arg1, arg2, arg3, arg4] = args
172171
let options = {} as NumberOptions
173172
let orverrides = {} as Intl.NumberFormatOptions
@@ -193,7 +192,7 @@ export function parseNumberArgs(
193192
orverrides = arg4
194193
}
195194

196-
return [value, options, orverrides]
195+
return [options.key || '', value, options, orverrides]
197196
}
198197

199198
export function clearNumberFormat(

0 commit comments

Comments
 (0)