From c56e69ec48250c16452d64ca831d8e447216019f Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Mon, 23 Nov 2020 11:00:59 -0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20easier=20to=20unders?= =?UTF-8?q?tand=20browser=20entrypoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/runtime/index.ts | 69 ++++++++++++++++---------- src/runtime/stores/formatters.ts | 2 +- test/runtime/stores/formatters.test.ts | 5 +- 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/runtime/index.ts b/src/runtime/index.ts index 684f6db..23c1c5b 100644 --- a/src/runtime/index.ts +++ b/src/runtime/index.ts @@ -1,7 +1,29 @@ import { MessageObject } from './types'; -import { getCurrentLocale } from './stores/locale'; -import { getOptions } from './configs'; -import { flush } from './includes/loaderQueue'; +import { getCurrentLocale, $locale } from './stores/locale'; +import { getOptions, init } from './configs'; +import { flush, registerLocaleLoader } from './includes/loaderQueue'; +import { + getLocaleFromHostname, + getLocaleFromPathname, + getLocaleFromNavigator, + getLocaleFromQueryString, + getLocaleFromHash, +} from './includes/localeGetters'; +import { $dictionary, $locales, addMessages } from './stores/dictionary'; +import { $isLoading } from './stores/loading'; +import { + $format, + $formatDate, + $formatNumber, + $formatTime, + $getJSON, +} from './stores/formatters'; +import { + getDateFormatter, + getNumberFormatter, + getTimeFormatter, + getMessageFormatter, +} from './includes/formatters'; // defineMessages allow us to define and extract dynamic message ids export function defineMessages(i: Record) { @@ -12,40 +34,33 @@ export function waitLocale(locale?: string) { return flush(locale || getCurrentLocale() || getOptions().initialLocale); } -export { init } from './configs'; -export { - getLocaleFromHostname, - getLocaleFromPathname, - getLocaleFromNavigator, - getLocaleFromQueryString, - getLocaleFromHash, -} from './includes/localeGetters'; - -export { $locale as locale } from './stores/locale'; - export { + // setup + init, + addMessages, + registerLocaleLoader as register, + // stores + $locale as locale, $dictionary as dictionary, $locales as locales, - addMessages, -} from './stores/dictionary'; -export { registerLocaleLoader as register } from './includes/loaderQueue'; - -export { $isLoading as isLoading } from './stores/loading'; - -export { + $isLoading as isLoading, + // reactive methods $format as format, $format as _, $format as t, $formatDate as date, $formatNumber as number, $formatTime as time, - $json as json, -} from './stores/formatters'; - -// low-level -export { + $getJSON as json, + // low-level getDateFormatter, getNumberFormatter, getTimeFormatter, getMessageFormatter, -} from './includes/formatters'; + // utils + getLocaleFromHostname, + getLocaleFromPathname, + getLocaleFromNavigator, + getLocaleFromQueryString, + getLocaleFromHash, +}; diff --git a/src/runtime/stores/formatters.ts b/src/runtime/stores/formatters.ts index 8ccf343..328d3c3 100644 --- a/src/runtime/stores/formatters.ts +++ b/src/runtime/stores/formatters.ts @@ -90,4 +90,4 @@ export const $format = derived([$locale, $dictionary], () => formatMessage); export const $formatTime = derived([$locale], () => formatTime); export const $formatDate = derived([$locale], () => formatDate); export const $formatNumber = derived([$locale], () => formatNumber); -export const $json = derived([$locale, $dictionary], () => getJSON); +export const $getJSON = derived([$locale, $dictionary], () => getJSON); diff --git a/test/runtime/stores/formatters.test.ts b/test/runtime/stores/formatters.test.ts index 076d5d1..3462407 100644 --- a/test/runtime/stores/formatters.test.ts +++ b/test/runtime/stores/formatters.test.ts @@ -12,7 +12,7 @@ import { $formatTime, $formatDate, $formatNumber, - $json, + $getJSON, } from '../../../src/runtime/stores/formatters'; import { init } from '../../../src/runtime/configs'; import { addMessages } from '../../../src/runtime/stores/dictionary'; @@ -29,7 +29,7 @@ $locale.subscribe(() => { formatTime = get($formatTime); formatDate = get($formatDate); formatNumber = get($formatNumber); - getJSON = get($json); + getJSON = get($getJSON); }); addMessages('en', require('../../fixtures/en.json')); @@ -41,6 +41,7 @@ addMessages('pt-PT', require('../../fixtures/pt-PT.json')); beforeEach(() => { init({ fallbackLocale: 'en' }); }); + describe('format message', () => { it('formats a message by its id and the current locale', () => { expect(formatMessage({ id: 'form.field_1_name' })).toBe('Name');