@formatjs/fast-memoize 3.0.1 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +30 -15
- package/index.js +48 -53
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,32 +1,47 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Main
|
|
3
|
+
//
|
|
1
4
|
type Func = (...args: any[]) => any;
|
|
2
|
-
export interface Cache<
|
|
3
|
-
|
|
5
|
+
export interface Cache<
|
|
6
|
+
K,
|
|
7
|
+
V
|
|
8
|
+
> {
|
|
9
|
+
create: CacheCreateFunc<K, V>;
|
|
4
10
|
}
|
|
5
|
-
interface CacheCreateFunc<
|
|
6
|
-
|
|
11
|
+
interface CacheCreateFunc<
|
|
12
|
+
K,
|
|
13
|
+
V
|
|
14
|
+
> {
|
|
15
|
+
(): DefaultCache<K, V>;
|
|
7
16
|
}
|
|
8
|
-
interface DefaultCache<
|
|
9
|
-
|
|
10
|
-
|
|
17
|
+
interface DefaultCache<
|
|
18
|
+
K,
|
|
19
|
+
V
|
|
20
|
+
> {
|
|
21
|
+
get(key: K): V | undefined;
|
|
22
|
+
set(key: K, value: V | undefined): void;
|
|
11
23
|
}
|
|
12
24
|
export type Serializer = (args: any[]) => string;
|
|
13
25
|
export interface Options<F extends Func> {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
26
|
+
cache?: Cache<string, ReturnType<F>>;
|
|
27
|
+
serializer?: Serializer;
|
|
28
|
+
strategy?: MemoizeFunc<F>;
|
|
17
29
|
}
|
|
18
30
|
export interface ResolvedOptions<F extends Func> {
|
|
19
|
-
|
|
20
|
-
|
|
31
|
+
cache: Cache<string, ReturnType<F>>;
|
|
32
|
+
serializer: Serializer;
|
|
21
33
|
}
|
|
22
34
|
export interface MemoizeFunc<F extends Func> {
|
|
23
|
-
|
|
35
|
+
(fn: F, options?: Options<F>): F;
|
|
24
36
|
}
|
|
25
37
|
export declare function memoize<F extends Func>(fn: F, options?: Options<F>): F;
|
|
26
38
|
export type StrategyFn = <F extends Func>(this: unknown, fn: F, cache: DefaultCache<string, ReturnType<F>>, serializer: Serializer, arg: any) => any;
|
|
39
|
+
//
|
|
40
|
+
// API
|
|
41
|
+
//
|
|
27
42
|
export interface Strategies<F extends Func> {
|
|
28
|
-
|
|
29
|
-
|
|
43
|
+
variadic: MemoizeFunc<F>;
|
|
44
|
+
monadic: MemoizeFunc<F>;
|
|
30
45
|
}
|
|
31
46
|
export declare const strategies: Strategies<Func>;
|
|
32
47
|
export {};
|
package/index.js
CHANGED
|
@@ -1,80 +1,75 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Main
|
|
3
|
-
//
|
|
4
1
|
export function memoize(fn, options) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
const cache = options && options.cache ? options.cache : cacheDefault;
|
|
3
|
+
const serializer = options && options.serializer ? options.serializer : serializerDefault;
|
|
4
|
+
const strategy = options && options.strategy ? options.strategy : strategyDefault;
|
|
5
|
+
return strategy(fn, {
|
|
6
|
+
cache,
|
|
7
|
+
serializer
|
|
8
|
+
});
|
|
12
9
|
}
|
|
13
10
|
//
|
|
14
11
|
// Strategy
|
|
15
12
|
//
|
|
16
13
|
function isPrimitive(value) {
|
|
17
|
-
|
|
14
|
+
return value == null || typeof value === "number" || typeof value === "boolean";
|
|
18
15
|
}
|
|
19
16
|
function monadic(fn, cache, serializer, arg) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
const cacheKey = isPrimitive(arg) ? arg : serializer(arg);
|
|
18
|
+
let computedValue = cache.get(cacheKey);
|
|
19
|
+
if (typeof computedValue === "undefined") {
|
|
20
|
+
computedValue = fn.call(this, arg);
|
|
21
|
+
cache.set(cacheKey, computedValue);
|
|
22
|
+
}
|
|
23
|
+
return computedValue;
|
|
27
24
|
}
|
|
28
25
|
function variadic(fn, cache, serializer) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
26
|
+
const args = Array.prototype.slice.call(arguments, 3);
|
|
27
|
+
const cacheKey = serializer(args);
|
|
28
|
+
let computedValue = cache.get(cacheKey);
|
|
29
|
+
if (typeof computedValue === "undefined") {
|
|
30
|
+
computedValue = fn.apply(this, args);
|
|
31
|
+
cache.set(cacheKey, computedValue);
|
|
32
|
+
}
|
|
33
|
+
return computedValue;
|
|
37
34
|
}
|
|
38
35
|
function assemble(fn, context, strategy, cache, serialize) {
|
|
39
|
-
|
|
36
|
+
return strategy.bind(context, fn, cache, serialize);
|
|
40
37
|
}
|
|
41
38
|
function strategyDefault(fn, options) {
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
const strategy = fn.length === 1 ? monadic : variadic;
|
|
40
|
+
return assemble(fn, this, strategy, options.cache.create(), options.serializer);
|
|
44
41
|
}
|
|
45
42
|
function strategyVariadic(fn, options) {
|
|
46
|
-
|
|
43
|
+
return assemble(fn, this, variadic, options.cache.create(), options.serializer);
|
|
47
44
|
}
|
|
48
45
|
function strategyMonadic(fn, options) {
|
|
49
|
-
|
|
46
|
+
return assemble(fn, this, monadic, options.cache.create(), options.serializer);
|
|
50
47
|
}
|
|
51
48
|
//
|
|
52
49
|
// Serializer
|
|
53
50
|
//
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
const serializerDefault = function() {
|
|
52
|
+
return JSON.stringify(arguments);
|
|
56
53
|
};
|
|
57
54
|
//
|
|
58
55
|
// Cache
|
|
59
56
|
//
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
variadic: strategyVariadic,
|
|
79
|
-
monadic: strategyMonadic,
|
|
57
|
+
class ObjectWithoutPrototypeCache {
|
|
58
|
+
cache;
|
|
59
|
+
constructor() {
|
|
60
|
+
this.cache = Object.create(null);
|
|
61
|
+
}
|
|
62
|
+
get(key) {
|
|
63
|
+
return this.cache[key];
|
|
64
|
+
}
|
|
65
|
+
set(key, value) {
|
|
66
|
+
this.cache[key] = value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const cacheDefault = { create: function create() {
|
|
70
|
+
return new ObjectWithoutPrototypeCache();
|
|
71
|
+
} };
|
|
72
|
+
export const strategies = {
|
|
73
|
+
variadic: strategyVariadic,
|
|
74
|
+
monadic: strategyMonadic
|
|
80
75
|
};
|