Skip to content

Commit

Permalink
fix: memoizing of formatters when no locale is given. (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkert committed Jan 29, 2020
1 parent 88f7762 commit 27871f9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/runtime/includes/formatters.ts
Expand Up @@ -18,11 +18,10 @@ const getIntlFormatterOptions = (
throw new Error(`[svelte-i18n] Unknown "${name}" ${type} format.`)
}

export const getNumberFormatter: MemoizedIntlFormatter<
const createNumberFormatter: MemoizedIntlFormatter<
Intl.NumberFormat,
Intl.NumberFormatOptions
> = monadicMemoize(({ locale, format, ...options } = {}) => {
locale = locale || getCurrentLocale()
> = monadicMemoize(({ locale, format, ...options }) => {
if (locale == null) {
throw new Error('[svelte-i18n] A "locale" must be set to format numbers')
}
Expand All @@ -33,12 +32,12 @@ export const getNumberFormatter: MemoizedIntlFormatter<

return new Intl.NumberFormat(locale, options)
})
export const getNumberFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createNumberFormatter({ locale, ...args })

export const getDateFormatter: MemoizedIntlFormatter<
const createDateFormatter: MemoizedIntlFormatter<
Intl.DateTimeFormat,
Intl.DateTimeFormatOptions
> = monadicMemoize(({ locale, format, ...options } = {}) => {
locale = locale || getCurrentLocale()
> = monadicMemoize(({ locale, format, ...options }) => {
if (locale == null) {
throw new Error('[svelte-i18n] A "locale" must be set to format dates')
}
Expand All @@ -50,12 +49,12 @@ export const getDateFormatter: MemoizedIntlFormatter<

return new Intl.DateTimeFormat(locale, options)
})
export const getDateFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createDateFormatter({ locale, ...args })

export const getTimeFormatter: MemoizedIntlFormatter<
const createTimeFormatter: MemoizedIntlFormatter<
Intl.DateTimeFormat,
Intl.DateTimeFormatOptions
> = monadicMemoize(({ locale, format, ...options } = {}) => {
locale = locale || getCurrentLocale()
> = monadicMemoize(({ locale, format, ...options }) => {
if (locale == null) {
throw new Error(
'[svelte-i18n] A "locale" must be set to format time values'
Expand All @@ -69,6 +68,7 @@ export const getTimeFormatter: MemoizedIntlFormatter<

return new Intl.DateTimeFormat(locale, options)
})
export const getTimeFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createTimeFormatter({ locale, ...args })

export const getMessageFormatter = monadicMemoize(
(message: string, locale: string = getCurrentLocale()) =>
Expand Down
29 changes: 27 additions & 2 deletions test/runtime/includes/formatters.test.ts
Expand Up @@ -3,7 +3,8 @@ import {
getDateFormatter,
getTimeFormatter,
getMessageFormatter,
init
init,
locale
} from '../../../src/runtime'

beforeEach(() => {
Expand All @@ -19,7 +20,7 @@ describe('number formatter', () => {
)
})

test('formats a date according to the current locale', () => {
test('formats a number according to the current locale', () => {
init({ fallbackLocale: 'en' })
expect(getNumberFormatter().format(number)).toBe('123,123')
})
Expand Down Expand Up @@ -49,6 +50,14 @@ describe('number formatter', () => {
getNumberFormatter({ style: 'currency', currency: 'BRL' }).format(number)
).toBe('R$123,123.00')
})

test('formats a number according to the currently set locale', () => {
locale.set('en')
expect(getNumberFormatter().format(number)).toBe('123,123')

locale.set('nl')
expect(getNumberFormatter().format(number)).toBe('123.123')
})
})

describe('date formatter', () => {
Expand Down Expand Up @@ -98,6 +107,14 @@ describe('date formatter', () => {
getDateFormatter({ year: 'numeric', era: 'short' }).format(date)
).toBe('2019 AD')
})

test('formats a date according to the currently set locale', () => {
locale.set('en')
expect(getDateFormatter().format(date)).toBe('2/1/19')

locale.set('nl')
expect(getDateFormatter().format(date)).toBe('1-2-19')
})
})

describe('time formatter', () => {
Expand Down Expand Up @@ -151,6 +168,14 @@ describe('time formatter', () => {
}).format(time)
).toBe('08:37:32 PM')
})

test('formats time according to the currently set locale', () => {
locale.set('en')
expect(getTimeFormatter().format(time)).toBe('8:37 PM')

locale.set('nl')
expect(getTimeFormatter().format(time)).toBe('20:37')
})
})

describe('message formatter', () => {
Expand Down

0 comments on commit 27871f9

Please sign in to comment.