diff --git a/package.json b/package.json index 8d6eb57..5ec307b 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,6 @@ "dependencies": { "commander": "^4.0.1", "estree-walker": "^0.9.0", - "fast-memoize": "^2.5.1", "intl-messageformat": "^7.5.2", "tiny-glob": "^0.2.6" } diff --git a/src/client/includes/formatters.ts b/src/client/includes/formatters.ts index 35b226b..babcbbf 100644 --- a/src/client/includes/formatters.ts +++ b/src/client/includes/formatters.ts @@ -1,10 +1,11 @@ import IntlMessageFormat from 'intl-messageformat' -import memoize from 'fast-memoize' import { MemoizedIntlFormatter } from '../types' import { getCurrentLocale } from '../stores/locale' import { getOptions } from '../configs' +import { monadicMemoize } from './memoize' + const getIntlFormatterOptions = ( type: 'time' | 'number' | 'date', name: string @@ -20,7 +21,7 @@ const getIntlFormatterOptions = ( export const getNumberFormatter: MemoizedIntlFormatter< Intl.NumberFormat, Intl.NumberFormatOptions -> = memoize(({ locale, format, ...options } = {}) => { +> = monadicMemoize(({ locale, format, ...options } = {}) => { locale = locale || getCurrentLocale() if (locale == null) { throw new Error('[svelte-i18n] A "locale" must be set to format numbers') @@ -36,7 +37,7 @@ export const getNumberFormatter: MemoizedIntlFormatter< export const getDateFormatter: MemoizedIntlFormatter< Intl.DateTimeFormat, Intl.DateTimeFormatOptions -> = memoize(({ locale, format, ...options } = {}) => { +> = monadicMemoize(({ locale, format, ...options } = {}) => { locale = locale || getCurrentLocale() if (locale == null) { throw new Error('[svelte-i18n] A "locale" must be set to format dates') @@ -53,7 +54,7 @@ export const getDateFormatter: MemoizedIntlFormatter< export const getTimeFormatter: MemoizedIntlFormatter< Intl.DateTimeFormat, Intl.DateTimeFormatOptions -> = memoize(({ locale, format, ...options } = {}) => { +> = monadicMemoize(({ locale, format, ...options } = {}) => { locale = locale || getCurrentLocale() if (locale == null) { throw new Error( @@ -69,7 +70,7 @@ export const getTimeFormatter: MemoizedIntlFormatter< return new Intl.DateTimeFormat(locale, options) }) -export const getMessageFormatter = memoize( +export const getMessageFormatter = monadicMemoize( (message: string, locale: string = getCurrentLocale()) => new IntlMessageFormat(message, locale, getOptions().formats) ) diff --git a/src/client/includes/memoize.ts b/src/client/includes/memoize.ts new file mode 100644 index 0000000..956ffc6 --- /dev/null +++ b/src/client/includes/memoize.ts @@ -0,0 +1,14 @@ +type MemoizedFunction = (fn: F) => F + +const monadicMemoize: MemoizedFunction = fn => { + const cache = Object.create(null) + const memoizedFn: any = (arg: unknown) => { + const cacheKey = JSON.stringify(arg) + if (cacheKey in cache) { + return cache[cacheKey] + } + return (cache[cacheKey] = fn(arg)) + } + return memoizedFn +} +export { monadicMemoize } diff --git a/yarn.lock b/yarn.lock index 7140506..04511ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2382,11 +2382,6 @@ fast-levenshtein@~2.0.6: resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917" integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc= -fast-memoize@^2.5.1: - version "2.5.1" - resolved "https://registry.yarnpkg.com/fast-memoize/-/fast-memoize-2.5.1.tgz#c3519241e80552ce395e1a32dcdde8d1fd680f5d" - integrity sha512-xdmw296PCL01tMOXx9mdJSmWY29jQgxyuZdq0rEHMu+Tpe1eOEtCycoG6chzlcrWsNgpZP7oL8RiQr7+G6Bl6g== - fb-watchman@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.1.tgz#fc84fb39d2709cf3ff6d743706157bb5708a8a85"