refactor: 💡 remove fast-memoize in favour of simple monadic fn

This commit is contained in:
Christian Kaisermann 2020-01-15 22:54:10 -03:00
parent 69ec477ffd
commit 932847be2d
4 changed files with 20 additions and 11 deletions

View File

@ -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"
}

View File

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

View File

@ -0,0 +1,14 @@
type MemoizedFunction = <F extends any>(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 }

View File

@ -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"