From 8235f1e078c299b14c339cf0ed9ee1366efdf34e Mon Sep 17 00:00:00 2001 From: Luma Date: Tue, 24 Aug 2021 11:59:40 +0900 Subject: [PATCH] chore: fixes for 1st review --- src/runtime/configs.ts | 6 ++++- src/runtime/includes/formatters.ts | 7 +++--- src/runtime/includes/loaderQueue.ts | 4 +-- src/runtime/stores/dictionary.ts | 4 +-- src/runtime/stores/formatters.ts | 2 +- src/runtime/stores/locale.ts | 31 +++++++++++++----------- src/runtime/types/index.ts | 17 ++++++------- test/runtime/configs.test.ts | 2 +- test/runtime/includes/formatters.test.ts | 2 +- test/runtime/stores/locale.test.ts | 4 +-- 10 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/runtime/configs.ts b/src/runtime/configs.ts index 204ca17..b502ced 100644 --- a/src/runtime/configs.ts +++ b/src/runtime/configs.ts @@ -38,7 +38,11 @@ export const defaultFormats: Formats = { }, }; -export const defaultOptions: ConfigureOptions = { +export const defaultOptions: Omit< + ConfigureOptions, + 'fallbackLocale' | 'initialLocale' +> & + Record<'fallbackLocale' | 'initialLocale', null> = { fallbackLocale: null, initialLocale: null, loadingDelay: 200, diff --git a/src/runtime/includes/formatters.ts b/src/runtime/includes/formatters.ts index 46dd2ad..bb45eaa 100644 --- a/src/runtime/includes/formatters.ts +++ b/src/runtime/includes/formatters.ts @@ -1,3 +1,4 @@ +import type { Formats } from 'intl-messageformat'; import IntlMessageFormat from 'intl-messageformat'; import type { @@ -34,8 +35,8 @@ const getIntlFormatterOptions = ( ): any => { const { formats } = getOptions(); - if (type in formats && name in formats[type]) { - return formats[type][name]; + if (type in formats && name in (formats as Formats)[type]) { + return (formats as Formats)[type][name]; } throw new Error(`[svelte-i18n] Unknown "${name}" ${type} format.`); @@ -105,7 +106,7 @@ export const getTimeFormatter: MemoizedDateTimeFormatterFactoryOptional = ({ } = {}) => createTimeFormatter({ locale, ...args }); export const getMessageFormatter = monadicMemoize( - (message: string, locale: string = getCurrentLocale()) => + (message: string, locale: string = getCurrentLocale()!) => new IntlMessageFormat(message, locale, getOptions().formats, { ignoreTag: getOptions().ignoreTag, }), diff --git a/src/runtime/includes/loaderQueue.ts b/src/runtime/includes/loaderQueue.ts index 007d892..1a6b42b 100644 --- a/src/runtime/includes/loaderQueue.ts +++ b/src/runtime/includes/loaderQueue.ts @@ -62,13 +62,13 @@ function loadLocaleQueue(locale: string, localeQueue: MessagesLoader[]) { const activeFlushes: { [key: string]: Promise } = {}; -export async function flush(locale: string): Promise { +export function flush(locale: string): Promise { if (!hasLocaleQueue(locale)) { if (locale in activeFlushes) { return activeFlushes[locale]; } - return; + return Promise.resolve(); } // get queue of XX-YY and XX locales diff --git a/src/runtime/stores/dictionary.ts b/src/runtime/stores/dictionary.ts index 701c385..bf7fa28 100644 --- a/src/runtime/stores/dictionary.ts +++ b/src/runtime/stores/dictionary.ts @@ -35,8 +35,8 @@ export function getMessageFromDictionary(locale: string, id: string) { } export function getClosestAvailableLocale( - refLocale: string, -): string | null | undefined { + refLocale: string | null | undefined, +): string | undefined { if (refLocale == null) return undefined; const relatedLocales = getPossibleLocales(refLocale); diff --git a/src/runtime/stores/formatters.ts b/src/runtime/stores/formatters.ts index 12af044..c34303c 100644 --- a/src/runtime/stores/formatters.ts +++ b/src/runtime/stores/formatters.ts @@ -47,7 +47,7 @@ const formatMessage: MessageFormatter = (id, options = {}) => { `[svelte-i18n] The message "${id}" was not found in "${getPossibleLocales( locale, ).join('", "')}".${ - hasLocaleQueue(getCurrentLocale()) + hasLocaleQueue(getCurrentLocale()!) ? `\n\nNote: there are at least one loader still registered to this locale that wasn't executed.` : '' }`, diff --git a/src/runtime/stores/locale.ts b/src/runtime/stores/locale.ts index 04cfc96..441784b 100644 --- a/src/runtime/stores/locale.ts +++ b/src/runtime/stores/locale.ts @@ -5,8 +5,8 @@ import { getOptions } from '../configs'; import { getClosestAvailableLocale } from './dictionary'; import { $isLoading } from './loading'; -let current: string; -const $locale = writable(null); +let current: string | null | undefined; +const internalLocale = writable(null); function getSubLocales(refLocale: string) { return refLocale @@ -32,18 +32,21 @@ export function getCurrentLocale() { return current; } -$locale.subscribe((newLocale: string) => { +internalLocale.subscribe((newLocale: string | null | undefined) => { current = newLocale; - if (typeof window !== 'undefined' && newLocale !== null) { + if (typeof window !== 'undefined' && newLocale != null) { document.documentElement.setAttribute('lang', newLocale); } }); -const localeSet = $locale.set; - -$locale.set = (newLocale: string): void | Promise => { - if (getClosestAvailableLocale(newLocale) && hasLocaleQueue(newLocale)) { +const set = (newLocale: string | null | undefined): void | Promise => { + if ( + ((getClosestAvailableLocale as unknown) as ( + refLocale: string | null | undefined, + ) => refLocale is string)(newLocale) && + hasLocaleQueue(newLocale) + ) { const { loadingDelay } = getOptions(); let loadingTimer: number; @@ -65,7 +68,7 @@ $locale.set = (newLocale: string): void | Promise => { return flush(newLocale) .then(() => { - localeSet(newLocale); + internalLocale.set(newLocale); }) .finally(() => { clearTimeout(loadingTimer); @@ -73,12 +76,12 @@ $locale.set = (newLocale: string): void | Promise => { }); } - return localeSet(newLocale); + return internalLocale.set(newLocale); }; -// istanbul ignore next -$locale.update = ( - fn: (value: string | null | undefined) => string | null | undefined, -) => localeSet(fn(current)); +const $locale = { + ...internalLocale, + set, +}; export { $locale }; diff --git a/src/runtime/types/index.ts b/src/runtime/types/index.ts index a2b5e1b..17cb1d6 100644 --- a/src/runtime/types/index.ts +++ b/src/runtime/types/index.ts @@ -1,4 +1,4 @@ -import type { FormatXMLElementFn, Formats } from 'intl-messageformat'; +import type { Formats, FormatXMLElementFn } from 'intl-messageformat'; export interface LocaleDictionary { [key: string]: @@ -27,7 +27,7 @@ export type InterpolationValues = export interface MessageObject { id?: string; - locale?: string; + locale?: string | null; format?: string; default?: string; values?: InterpolationValues; @@ -53,11 +53,11 @@ export type NumberFormatter = ( options?: IntlFormatterOptions, ) => string; -export type JSONGetter = (id: string, locale?: string) => any; +export type JSONGetter = (id: string, locale?: string | null) => any; type IntlFormatterOptions = T & { format?: string; - locale?: string; + locale?: string | null; }; export interface MemoizedIntlFormatter { @@ -73,14 +73,13 @@ export interface MessagesLoader { } export interface ConfigureOptions { - fallbackLocale: string | null | undefined; - formats: Formats; - initialLocale: string | null; + fallbackLocale: string; + formats: Partial; + initialLocale: string; loadingDelay: number; warnOnMissingMessages: boolean; ignoreTag: boolean; } export type ConfigureOptionsInit = Pick & - Partial>> & - Partial>; + Partial>; diff --git a/test/runtime/configs.test.ts b/test/runtime/configs.test.ts index 4758262..e182ad4 100644 --- a/test/runtime/configs.test.ts +++ b/test/runtime/configs.test.ts @@ -10,7 +10,7 @@ import { import { $locale } from '../../src/runtime/stores/locale'; beforeEach(() => { - init(defaultOptions); + init(defaultOptions as any); }); test('inits the fallback locale', () => { diff --git a/test/runtime/includes/formatters.test.ts b/test/runtime/includes/formatters.test.ts index f40c138..e37966d 100644 --- a/test/runtime/includes/formatters.test.ts +++ b/test/runtime/includes/formatters.test.ts @@ -10,7 +10,7 @@ import { const formatsJson = require('../../fixtures/formats.json'); beforeEach(() => { - init({ fallbackLocale: undefined }); + init({ fallbackLocale: undefined as any }); }); describe('number formatter', () => { diff --git a/test/runtime/stores/locale.test.ts b/test/runtime/stores/locale.test.ts index 05642c3..e5b9547 100644 --- a/test/runtime/stores/locale.test.ts +++ b/test/runtime/stores/locale.test.ts @@ -11,7 +11,7 @@ import { register, isLoading } from '../../../src/runtime'; import { hasLocaleQueue } from '../../../src/runtime/includes/loaderQueue'; beforeEach(() => { - init({ fallbackLocale: undefined }); + init({ fallbackLocale: undefined as any }); $locale.set(undefined); }); @@ -90,7 +90,7 @@ test('if no initial locale is set, set the locale to the fallback', () => { test('if no initial locale was found, set to the fallback locale', () => { init({ fallbackLocale: 'en', - initialLocale: null, + initialLocale: null as any, }); expect(get($locale)).toBe('en'); expect(getOptions().fallbackLocale).toBe('en');