From e400e62a6294d21f0b8db0e2081e0c9e53aa5c20 Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Wed, 29 Jan 2020 11:46:34 -0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20add=20correct=20typi?= =?UTF-8?q?ngs=20for=20formatter=20getters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/runtime/includes/formatters.ts | 103 +++++++++++++++++------------ 1 file changed, 61 insertions(+), 42 deletions(-) diff --git a/src/runtime/includes/formatters.ts b/src/runtime/includes/formatters.ts index 3c9ccfb..69f9214 100644 --- a/src/runtime/includes/formatters.ts +++ b/src/runtime/includes/formatters.ts @@ -6,6 +6,16 @@ import { getOptions } from '../configs' import { monadicMemoize } from './memoize' +type MemoizedNumberFormatterFactory = MemoizedIntlFormatter< + Intl.NumberFormat, + Intl.NumberFormatOptions +> + +type MemoizedDateTimeFormatterFactory = MemoizedIntlFormatter< + Intl.DateTimeFormat, + Intl.DateTimeFormatOptions +> + const getIntlFormatterOptions = ( type: 'time' | 'number' | 'date', name: string @@ -18,57 +28,66 @@ const getIntlFormatterOptions = ( throw new Error(`[svelte-i18n] Unknown "${name}" ${type} format.`) } -const createNumberFormatter: MemoizedIntlFormatter< - Intl.NumberFormat, - Intl.NumberFormatOptions -> = monadicMemoize(({ locale, format, ...options }) => { - if (locale == null) { - throw new Error('[svelte-i18n] A "locale" must be set to format numbers') +const createNumberFormatter: MemoizedNumberFormatterFactory = monadicMemoize( + ({ locale, format, ...options }) => { + 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) { - options = getIntlFormatterOptions('number', format) +const createDateFormatter: MemoizedDateTimeFormatterFactory = monadicMemoize( + ({ 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) -}) -export const getNumberFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createNumberFormatter({ locale, ...args }) +const createTimeFormatter: MemoizedDateTimeFormatterFactory = monadicMemoize( + ({ locale, format, ...options }) => { + if (locale == null) { + throw new Error( + '[svelte-i18n] A "locale" must be set to format time values' + ) + } -const createDateFormatter: MemoizedIntlFormatter< - Intl.DateTimeFormat, - Intl.DateTimeFormatOptions -> = monadicMemoize(({ locale, format, ...options }) => { - if (locale == null) { - throw new Error('[svelte-i18n] A "locale" must be set to format dates') + if (format) options = getIntlFormatterOptions('time', format) + else if (Object.keys(options).length === 0) { + options = getIntlFormatterOptions('time', 'short') + } + + return new Intl.DateTimeFormat(locale, options) } +) - if (format) options = getIntlFormatterOptions('date', format) - else if (Object.keys(options).length === 0) { - options = getIntlFormatterOptions('date', 'short') - } +export const getNumberFormatter: MemoizedNumberFormatterFactory = ({ + locale = getCurrentLocale(), + ...args +} = {}) => createNumberFormatter({ locale, ...args }) - return new Intl.DateTimeFormat(locale, options) -}) -export const getDateFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createDateFormatter({ locale, ...args }) +export const getDateFormatter: MemoizedDateTimeFormatterFactory = ({ + locale = getCurrentLocale(), + ...args +} = {}) => createDateFormatter({ locale, ...args }) -const createTimeFormatter: MemoizedIntlFormatter< - Intl.DateTimeFormat, - Intl.DateTimeFormatOptions -> = monadicMemoize(({ locale, format, ...options }) => { - 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 getTimeFormatter: MemoizedDateTimeFormatterFactory = ({ + locale = getCurrentLocale(), + ...args +} = {}) => createTimeFormatter({ locale, ...args }) export const getMessageFormatter = monadicMemoize( (message: string, locale: string = getCurrentLocale()) =>