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,10 +28,8 @@ 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
> = monadicMemoize(({ locale, format, ...options }) => {
if (locale == null) { if (locale == null) {
throw new Error('[svelte-i18n] A "locale" must be set to format numbers') throw new Error('[svelte-i18n] A "locale" must be set to format numbers')
} }
@ -31,13 +39,11 @@ const createNumberFormatter: MemoizedIntlFormatter<
} }
return new Intl.NumberFormat(locale, options) return new Intl.NumberFormat(locale, options)
}) }
export const getNumberFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createNumberFormatter({ locale, ...args }) )
const createDateFormatter: MemoizedIntlFormatter< const createDateFormatter: MemoizedDateTimeFormatterFactory = monadicMemoize(
Intl.DateTimeFormat, ({ locale, format, ...options }) => {
Intl.DateTimeFormatOptions
> = monadicMemoize(({ locale, format, ...options }) => {
if (locale == null) { if (locale == null) {
throw new Error('[svelte-i18n] A "locale" must be set to format dates') throw new Error('[svelte-i18n] A "locale" must be set to format dates')
} }
@ -48,13 +54,11 @@ const createDateFormatter: MemoizedIntlFormatter<
} }
return new Intl.DateTimeFormat(locale, options) return new Intl.DateTimeFormat(locale, options)
}) }
export const getDateFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createDateFormatter({ locale, ...args }) )
const createTimeFormatter: MemoizedIntlFormatter< const createTimeFormatter: MemoizedDateTimeFormatterFactory = monadicMemoize(
Intl.DateTimeFormat, ({ locale, format, ...options }) => {
Intl.DateTimeFormatOptions
> = monadicMemoize(({ locale, format, ...options }) => {
if (locale == null) { if (locale == null) {
throw new Error( throw new Error(
'[svelte-i18n] A "locale" must be set to format time values' '[svelte-i18n] A "locale" must be set to format time values'
@ -67,8 +71,23 @@ const createTimeFormatter: MemoizedIntlFormatter<
} }
return new Intl.DateTimeFormat(locale, options) return new Intl.DateTimeFormat(locale, options)
}) }
export const getTimeFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createTimeFormatter({ locale, ...args }) )
export const getNumberFormatter: MemoizedNumberFormatterFactory = ({
locale = getCurrentLocale(),
...args
} = {}) => createNumberFormatter({ locale, ...args })
export const getDateFormatter: MemoizedDateTimeFormatterFactory = ({
locale = getCurrentLocale(),
...args
} = {}) => createDateFormatter({ locale, ...args })
export const getTimeFormatter: MemoizedDateTimeFormatterFactory = ({
locale = getCurrentLocale(),
...args
} = {}) => createTimeFormatter({ locale, ...args })
export const getMessageFormatter = monadicMemoize( export const getMessageFormatter = monadicMemoize(
(message: string, locale: string = getCurrentLocale()) => (message: string, locale: string = getCurrentLocale()) =>