refactor: 💡 add correct typings for formatter getters

This commit is contained in:
Christian Kaisermann 2020-01-29 11:46:34 -03:00
parent 27871f9775
commit e400e62a62

View File

@ -6,6 +6,16 @@ import { getOptions } from '../configs'
import { monadicMemoize } from './memoize' import { monadicMemoize } from './memoize'
type MemoizedNumberFormatterFactory = MemoizedIntlFormatter<
Intl.NumberFormat,
Intl.NumberFormatOptions
>
type MemoizedDateTimeFormatterFactory = MemoizedIntlFormatter<
Intl.DateTimeFormat,
Intl.DateTimeFormatOptions
>
const getIntlFormatterOptions = ( const getIntlFormatterOptions = (
type: 'time' | 'number' | 'date', type: 'time' | 'number' | 'date',
name: string name: string
@ -18,57 +28,66 @@ const getIntlFormatterOptions = (
throw new Error(`[svelte-i18n] Unknown "${name}" ${type} format.`) throw new Error(`[svelte-i18n] Unknown "${name}" ${type} format.`)
} }
const createNumberFormatter: MemoizedIntlFormatter< const createNumberFormatter: MemoizedNumberFormatterFactory = monadicMemoize(
Intl.NumberFormat, ({ locale, format, ...options }) => {
Intl.NumberFormatOptions if (locale == null) {
> = monadicMemoize(({ locale, format, ...options }) => { throw new Error('[svelte-i18n] A "locale" must be set to format numbers')
if (locale == null) { }
throw new Error('[svelte-i18n] A "locale" must be set to format numbers')
if (format) {
options = getIntlFormatterOptions('number', format)
}
return new Intl.NumberFormat(locale, options)
} }
)
if (format) { const createDateFormatter: MemoizedDateTimeFormatterFactory = monadicMemoize(
options = getIntlFormatterOptions('number', format) ({ locale, format, ...options }) => {
if (locale == null) {
throw new Error('[svelte-i18n] A "locale" must be set to format dates')
}
if (format) options = getIntlFormatterOptions('date', format)
else if (Object.keys(options).length === 0) {
options = getIntlFormatterOptions('date', 'short')
}
return new Intl.DateTimeFormat(locale, options)
} }
)
return new Intl.NumberFormat(locale, options) const createTimeFormatter: MemoizedDateTimeFormatterFactory = monadicMemoize(
}) ({ locale, format, ...options }) => {
export const getNumberFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createNumberFormatter({ locale, ...args }) if (locale == null) {
throw new Error(
'[svelte-i18n] A "locale" must be set to format time values'
)
}
const createDateFormatter: MemoizedIntlFormatter< if (format) options = getIntlFormatterOptions('time', format)
Intl.DateTimeFormat, else if (Object.keys(options).length === 0) {
Intl.DateTimeFormatOptions options = getIntlFormatterOptions('time', 'short')
> = monadicMemoize(({ locale, format, ...options }) => { }
if (locale == null) {
throw new Error('[svelte-i18n] A "locale" must be set to format dates') return new Intl.DateTimeFormat(locale, options)
} }
)
if (format) options = getIntlFormatterOptions('date', format) export const getNumberFormatter: MemoizedNumberFormatterFactory = ({
else if (Object.keys(options).length === 0) { locale = getCurrentLocale(),
options = getIntlFormatterOptions('date', 'short') ...args
} } = {}) => createNumberFormatter({ locale, ...args })
return new Intl.DateTimeFormat(locale, options) export const getDateFormatter: MemoizedDateTimeFormatterFactory = ({
}) locale = getCurrentLocale(),
export const getDateFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createDateFormatter({ locale, ...args }) ...args
} = {}) => createDateFormatter({ locale, ...args })
const createTimeFormatter: MemoizedIntlFormatter< export const getTimeFormatter: MemoizedDateTimeFormatterFactory = ({
Intl.DateTimeFormat, locale = getCurrentLocale(),
Intl.DateTimeFormatOptions ...args
> = monadicMemoize(({ locale, format, ...options }) => { } = {}) => createTimeFormatter({ locale, ...args })
if (locale == null) {
throw new Error(
'[svelte-i18n] A "locale" must be set to format time values'
)
}
if (format) options = getIntlFormatterOptions('time', format)
else if (Object.keys(options).length === 0) {
options = getIntlFormatterOptions('time', 'short')
}
return new Intl.DateTimeFormat(locale, options)
})
export const getTimeFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createTimeFormatter({ locale, ...args })
export const getMessageFormatter = monadicMemoize( export const getMessageFormatter = monadicMemoize(
(message: string, locale: string = getCurrentLocale()) => (message: string, locale: string = getCurrentLocale()) =>