Skip to content

Commit

Permalink
refactor: 💡 easier to understand browser entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed Nov 23, 2020
1 parent ae929eb commit 362e00a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
69 changes: 42 additions & 27 deletions src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -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>) {
Expand All @@ -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,
};
2 changes: 1 addition & 1 deletion src/runtime/stores/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 2 additions & 2 deletions test/runtime/stores/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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'));
Expand Down

0 comments on commit 362e00a

Please sign in to comment.