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 { 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<string, MessageObject>) {
@ -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,
};

View File

@ -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);

View File

@ -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');