refactor: 💡 easier to understand browser entrypoint

This commit is contained in:
Christian Kaisermann 2020-11-23 11:00:59 -03:00
parent 9dd1a19aef
commit 8db3ad6b4f
3 changed files with 46 additions and 30 deletions

View File

@ -1,7 +1,29 @@
import { MessageObject } from './types'; import { MessageObject } from './types';
import { getCurrentLocale } from './stores/locale'; import { getCurrentLocale, $locale } from './stores/locale';
import { getOptions } from './configs'; import { getOptions, init } from './configs';
import { flush } from './includes/loaderQueue'; 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 // defineMessages allow us to define and extract dynamic message ids
export function defineMessages(i: Record<string, MessageObject>) { export function defineMessages(i: Record<string, MessageObject>) {
@ -12,40 +34,33 @@ export function waitLocale(locale?: string) {
return flush(locale || getCurrentLocale() || getOptions().initialLocale); 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 { export {
// setup
init,
addMessages,
registerLocaleLoader as register,
// stores
$locale as locale,
$dictionary as dictionary, $dictionary as dictionary,
$locales as locales, $locales as locales,
addMessages, $isLoading as isLoading,
} from './stores/dictionary'; // reactive methods
export { registerLocaleLoader as register } from './includes/loaderQueue';
export { $isLoading as isLoading } from './stores/loading';
export {
$format as format, $format as format,
$format as _, $format as _,
$format as t, $format as t,
$formatDate as date, $formatDate as date,
$formatNumber as number, $formatNumber as number,
$formatTime as time, $formatTime as time,
$json as json, $getJSON as json,
} from './stores/formatters'; // low-level
// low-level
export {
getDateFormatter, getDateFormatter,
getNumberFormatter, getNumberFormatter,
getTimeFormatter, getTimeFormatter,
getMessageFormatter, getMessageFormatter,
} from './includes/formatters'; // utils
getLocaleFromHostname,
getLocaleFromPathname,
getLocaleFromNavigator,
getLocaleFromQueryString,
getLocaleFromHash,
};

View File

@ -90,4 +90,4 @@ export const $format = derived([$locale, $dictionary], () => formatMessage);
export const $formatTime = derived([$locale], () => formatTime); export const $formatTime = derived([$locale], () => formatTime);
export const $formatDate = derived([$locale], () => formatDate); export const $formatDate = derived([$locale], () => formatDate);
export const $formatNumber = derived([$locale], () => formatNumber); export const $formatNumber = derived([$locale], () => formatNumber);
export const $json = derived([$locale, $dictionary], () => getJSON); export const $getJSON = derived([$locale, $dictionary], () => getJSON);

View File

@ -12,7 +12,7 @@ import {
$formatTime, $formatTime,
$formatDate, $formatDate,
$formatNumber, $formatNumber,
$json, $getJSON,
} from '../../../src/runtime/stores/formatters'; } from '../../../src/runtime/stores/formatters';
import { init } from '../../../src/runtime/configs'; import { init } from '../../../src/runtime/configs';
import { addMessages } from '../../../src/runtime/stores/dictionary'; import { addMessages } from '../../../src/runtime/stores/dictionary';
@ -29,7 +29,7 @@ $locale.subscribe(() => {
formatTime = get($formatTime); formatTime = get($formatTime);
formatDate = get($formatDate); formatDate = get($formatDate);
formatNumber = get($formatNumber); formatNumber = get($formatNumber);
getJSON = get($json); getJSON = get($getJSON);
}); });
addMessages('en', require('../../fixtures/en.json')); addMessages('en', require('../../fixtures/en.json'));
@ -41,6 +41,7 @@ addMessages('pt-PT', require('../../fixtures/pt-PT.json'));
beforeEach(() => { beforeEach(() => {
init({ fallbackLocale: 'en' }); init({ fallbackLocale: 'en' });
}); });
describe('format message', () => { describe('format message', () => {
it('formats a message by its id and the current locale', () => { it('formats a message by its id and the current locale', () => {
expect(formatMessage({ id: 'form.field_1_name' })).toBe('Name'); expect(formatMessage({ id: 'form.field_1_name' })).toBe('Name');