-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·32 lines (24 loc) · 920 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { MEMOIZED, NOT_FOUND, VALUE_KEY } from "./constants";
import type { Memoize } from "./types";
const getFromCache = (cache, args) =>
args.every(arg => (cache = cache.get(arg))) ? (cache.has(VALUE_KEY) ? cache.get(VALUE_KEY) : NOT_FOUND) : NOT_FOUND
const setToCache = (cache, args, value) =>
args.reduce((a, arg) => a.get(arg) || a.set(arg, new Map()).get(arg), cache).set(VALUE_KEY, value)
const memo: Memoize = (fn, options = {}) => {
if (fn[MEMOIZED]) return fn[MEMOIZED];
const cache = new Map();
const memoized = (...args) => {
const storedValue = getFromCache(cache, args);
if (storedValue !== NOT_FOUND) {
return storedValue;
}
const value = fn(...args);
setToCache(cache, args, value)
if (options.onlyOnce) cache.clear()
return value
}
memoized.reset = () => cache.clear();
memoized[MEMOIZED] = memoized;
return memoized;
}
export default memo;