Skip to content

Commit 3ce6477

Browse files
authored
refactor(memoizeN): extract support code out of closure (#7805)
1 parent d638e58 commit 3ce6477

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/helpers/memoizeN.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,34 @@ import memoize from "lodash/memoize"
77
* storing the result based on the arguments provided to the memoized function.
88
*/
99

10-
const memoizeN = (fn, resolver = ((...args) => args)) => {
11-
const shallowArrayEquals = (a) => (b) => {
12-
return Array.isArray(a) && Array.isArray(b)
13-
&& a.length === b.length
14-
&& a.every((val, index) => val === b[index])
10+
const shallowArrayEquals = (a) => (b) => {
11+
return Array.isArray(a) && Array.isArray(b)
12+
&& a.length === b.length
13+
&& a.every((val, index) => val === b[index])
14+
}
15+
16+
const list = (...args) => args
17+
18+
class Cache extends Map {
19+
delete(key) {
20+
const keys = Array.from(this.keys())
21+
const foundKey = keys.find(shallowArrayEquals(key))
22+
return super.delete(foundKey)
1523
}
1624

17-
class Cache extends Map {
18-
delete(key) {
19-
const keys = Array.from(this.keys())
20-
const foundKey = keys.find(shallowArrayEquals(key))
21-
return super.delete(foundKey)
22-
}
23-
24-
get(key) {
25-
const keys = Array.from(this.keys())
26-
const foundKey = keys.find(shallowArrayEquals(key))
27-
return super.get(foundKey)
28-
}
29-
30-
has(key) {
31-
const keys = Array.from(this.keys())
32-
return keys.findIndex(shallowArrayEquals(key)) !== -1
33-
}
25+
get(key) {
26+
const keys = Array.from(this.keys())
27+
const foundKey = keys.find(shallowArrayEquals(key))
28+
return super.get(foundKey)
3429
}
3530

31+
has(key) {
32+
const keys = Array.from(this.keys())
33+
return keys.findIndex(shallowArrayEquals(key)) !== -1
34+
}
35+
}
36+
37+
const memoizeN = (fn, resolver = list) => {
3638
const { Cache: OriginalCache } = memoize
3739
memoize.Cache = Cache
3840

0 commit comments

Comments
 (0)