refactor(memoizeN): extract support code out of closure (#7805)

This commit is contained in:
Vladimir Gorej
2022-01-27 08:57:50 +01:00
committed by GitHub
parent d638e58527
commit 3ce6477007

View File

@@ -7,14 +7,15 @@ import memoize from "lodash/memoize"
* storing the result based on the arguments provided to the memoized function. * storing the result based on the arguments provided to the memoized function.
*/ */
const memoizeN = (fn, resolver = ((...args) => args)) => { const shallowArrayEquals = (a) => (b) => {
const shallowArrayEquals = (a) => (b) => {
return Array.isArray(a) && Array.isArray(b) return Array.isArray(a) && Array.isArray(b)
&& a.length === b.length && a.length === b.length
&& a.every((val, index) => val === b[index]) && a.every((val, index) => val === b[index])
} }
class Cache extends Map { const list = (...args) => args
class Cache extends Map {
delete(key) { delete(key) {
const keys = Array.from(this.keys()) const keys = Array.from(this.keys())
const foundKey = keys.find(shallowArrayEquals(key)) const foundKey = keys.find(shallowArrayEquals(key))
@@ -31,8 +32,9 @@ const memoizeN = (fn, resolver = ((...args) => args)) => {
const keys = Array.from(this.keys()) const keys = Array.from(this.keys())
return keys.findIndex(shallowArrayEquals(key)) !== -1 return keys.findIndex(shallowArrayEquals(key)) !== -1
} }
} }
const memoizeN = (fn, resolver = list) => {
const { Cache: OriginalCache } = memoize const { Cache: OriginalCache } = memoize
memoize.Cache = Cache memoize.Cache = Cache