@formatjs/fast-memoize 1.1.0 → 1.2.1
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 +0 -1
- package/index.d.ts.map +1 -1
- package/index.js +0 -3
- package/lib/index.d.ts +33 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +78 -0
- package/package.json +6 -3
package/index.d.ts
CHANGED
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../packages/fast-memoize/index.ts"],"names":[],"mappings":"AAIA,aAAK,IAAI,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAA;AAEnC,MAAM,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;IACzB,MAAM,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;CAC9B;AAED,UAAU,eAAe,CAAC,CAAC,EAAE,CAAC;IAC5B,IAAI,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;CACvB;AAED,UAAU,YAAY,CAAC,CAAC,EAAE,CAAC;IACzB,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAA;IACd,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../packages/fast-memoize/index.ts"],"names":[],"mappings":"AAIA,aAAK,IAAI,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAA;AAEnC,MAAM,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;IACzB,MAAM,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;CAC9B;AAED,UAAU,eAAe,CAAC,CAAC,EAAE,CAAC;IAC5B,IAAI,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC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ACxC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC,CAAA;IACxB,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC,CAAA;CACxB;AAED,eAAO,MAAM,UAAU,EAAE,UAAU,CAAC,IAAI,CAGvC,CAAA"}
|
package/index.js
CHANGED
|
@@ -64,9 +64,6 @@ var serializerDefault = function () {
|
|
|
64
64
|
function ObjectWithoutPrototypeCache() {
|
|
65
65
|
this.cache = Object.create(null);
|
|
66
66
|
}
|
|
67
|
-
ObjectWithoutPrototypeCache.prototype.has = function (key) {
|
|
68
|
-
return key in this.cache;
|
|
69
|
-
};
|
|
70
67
|
ObjectWithoutPrototypeCache.prototype.get = function (key) {
|
|
71
68
|
return this.cache[key];
|
|
72
69
|
};
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
declare type Func = (...args: any[]) => any;
|
|
2
|
+
export interface Cache<K, V> {
|
|
3
|
+
create: CacheCreateFunc<K, V>;
|
|
4
|
+
}
|
|
5
|
+
interface CacheCreateFunc<K, V> {
|
|
6
|
+
(): DefaultCache<K, V>;
|
|
7
|
+
}
|
|
8
|
+
interface DefaultCache<K, V> {
|
|
9
|
+
get(key: K): V;
|
|
10
|
+
set(key: K, value: V): void;
|
|
11
|
+
}
|
|
12
|
+
export declare type Serializer = (args: any[]) => string;
|
|
13
|
+
export interface Options<F extends Func> {
|
|
14
|
+
cache?: Cache<string, ReturnType<F>>;
|
|
15
|
+
serializer?: Serializer;
|
|
16
|
+
strategy?: MemoizeFunc<F>;
|
|
17
|
+
}
|
|
18
|
+
export interface ResolvedOptions<F extends Func> {
|
|
19
|
+
cache: Cache<string, ReturnType<F>>;
|
|
20
|
+
serializer: Serializer;
|
|
21
|
+
}
|
|
22
|
+
export interface MemoizeFunc<F extends Func> {
|
|
23
|
+
(fn: F, options?: Options<F>): F;
|
|
24
|
+
}
|
|
25
|
+
export default function memoize<F extends Func>(fn: F, options?: Options<F>): F | ((arg: any) => any);
|
|
26
|
+
export declare type StrategyFn = <F extends Func>(this: unknown, fn: F, cache: DefaultCache<string, ReturnType<F>>, serializer: Serializer, arg: any) => any;
|
|
27
|
+
export interface Strategies<F extends Func> {
|
|
28
|
+
variadic: MemoizeFunc<F>;
|
|
29
|
+
monadic: MemoizeFunc<F>;
|
|
30
|
+
}
|
|
31
|
+
export declare const strategies: Strategies<Func>;
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../../packages/fast-memoize/index.ts"],"names":[],"mappings":"AAIA,aAAK,IAAI,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAA;AAEnC,MAAM,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;IACzB,MAAM,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;CAC9B;AAED,UAAU,eAAe,CAAC,CAAC,EAAE,CAAC;IAC5B,IAAI,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;CACvB;AAED,UAAU,YAAY,CAAC,CAAC,EAAE,CAAC;IACzB,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAA;IACd,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAA;CAC5B;AAED,oBAAY,UAAU,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,MAAM,CAAA;AAEhD,MAAM,WAAW,OAAO,CAAC,CAAC,SAAS,IAAI;IACrC,KAAK,CAAC,EAAE,KAAK,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA;IACpC,UAAU,CAAC,EAAE,UAAU,CAAA;IACvB,QAAQ,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,CAAA;CAC1B;AAED,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,IAAI;IAC7C,KAAK,EAAE,KAAK,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA;IACnC,UAAU,EAAE,UAAU,CAAA;CACvB;AAED,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,IAAI;IACzC,CAAC,EAAE,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;CACjC;AAED,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,CAAC,SAAS,IAAI,EAAE,EAAE,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,2BAa1E;AAYD,oBAAY,UAAU,GAAG,CAAC,CAAC,SAAS,IAAI,EACtC,IAAI,EAAE,OAAO,EACb,EAAE,EAAE,CAAC,EACL,KAAK,EAAE,YAAY,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,EAC1C,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,GAAG,KACL,GAAG,CAAA;AA4HR,MAAM,WAAW,UAAU,CAAC,CAAC,SAAS,IAAI;IACxC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC,CAAA;IACxB,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC,CAAA;CACxB;AAED,eAAO,MAAM,UAAU,EAAE,UAAU,CAAC,IAAI,CAGvC,CAAA"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Main
|
|
3
|
+
//
|
|
4
|
+
export default function memoize(fn, options) {
|
|
5
|
+
var cache = options && options.cache ? options.cache : cacheDefault;
|
|
6
|
+
var serializer = options && options.serializer ? options.serializer : serializerDefault;
|
|
7
|
+
var strategy = options && options.strategy ? options.strategy : strategyDefault;
|
|
8
|
+
return strategy(fn, {
|
|
9
|
+
cache: cache,
|
|
10
|
+
serializer: serializer,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
//
|
|
14
|
+
// Strategy
|
|
15
|
+
//
|
|
16
|
+
function isPrimitive(value) {
|
|
17
|
+
return (value == null || typeof value === 'number' || typeof value === 'boolean'); // || typeof value === "string" 'unsafe' primitive for our needs
|
|
18
|
+
}
|
|
19
|
+
function monadic(fn, cache, serializer, arg) {
|
|
20
|
+
var cacheKey = isPrimitive(arg) ? arg : serializer(arg);
|
|
21
|
+
var computedValue = cache.get(cacheKey);
|
|
22
|
+
if (typeof computedValue === 'undefined') {
|
|
23
|
+
computedValue = fn.call(this, arg);
|
|
24
|
+
cache.set(cacheKey, computedValue);
|
|
25
|
+
}
|
|
26
|
+
return computedValue;
|
|
27
|
+
}
|
|
28
|
+
function variadic(fn, cache, serializer) {
|
|
29
|
+
var args = Array.prototype.slice.call(arguments, 3);
|
|
30
|
+
var cacheKey = serializer(args);
|
|
31
|
+
var computedValue = cache.get(cacheKey);
|
|
32
|
+
if (typeof computedValue === 'undefined') {
|
|
33
|
+
computedValue = fn.apply(this, args);
|
|
34
|
+
cache.set(cacheKey, computedValue);
|
|
35
|
+
}
|
|
36
|
+
return computedValue;
|
|
37
|
+
}
|
|
38
|
+
function assemble(fn, context, strategy, cache, serialize) {
|
|
39
|
+
return strategy.bind(context, fn, cache, serialize);
|
|
40
|
+
}
|
|
41
|
+
function strategyDefault(fn, options) {
|
|
42
|
+
var strategy = fn.length === 1 ? monadic : variadic;
|
|
43
|
+
return assemble(fn, this, strategy, options.cache.create(), options.serializer);
|
|
44
|
+
}
|
|
45
|
+
function strategyVariadic(fn, options) {
|
|
46
|
+
return assemble(fn, this, variadic, options.cache.create(), options.serializer);
|
|
47
|
+
}
|
|
48
|
+
function strategyMonadic(fn, options) {
|
|
49
|
+
return assemble(fn, this, monadic, options.cache.create(), options.serializer);
|
|
50
|
+
}
|
|
51
|
+
//
|
|
52
|
+
// Serializer
|
|
53
|
+
//
|
|
54
|
+
var serializerDefault = function () {
|
|
55
|
+
return JSON.stringify(arguments);
|
|
56
|
+
};
|
|
57
|
+
//
|
|
58
|
+
// Cache
|
|
59
|
+
//
|
|
60
|
+
function ObjectWithoutPrototypeCache() {
|
|
61
|
+
this.cache = Object.create(null);
|
|
62
|
+
}
|
|
63
|
+
ObjectWithoutPrototypeCache.prototype.get = function (key) {
|
|
64
|
+
return this.cache[key];
|
|
65
|
+
};
|
|
66
|
+
ObjectWithoutPrototypeCache.prototype.set = function (key, value) {
|
|
67
|
+
this.cache[key] = value;
|
|
68
|
+
};
|
|
69
|
+
var cacheDefault = {
|
|
70
|
+
create: function create() {
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
return new ObjectWithoutPrototypeCache();
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
export var strategies = {
|
|
76
|
+
variadic: strategyVariadic,
|
|
77
|
+
monadic: strategyMonadic,
|
|
78
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/fast-memoize",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "fork of fast-memoize and support esm",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -18,9 +18,12 @@
|
|
|
18
18
|
"i18n"
|
|
19
19
|
],
|
|
20
20
|
"author": "Long Ho <holevietlong@gmail.com>",
|
|
21
|
-
"license": "
|
|
21
|
+
"license": "MIT",
|
|
22
22
|
"bugs": {
|
|
23
23
|
"url": "https://github.com/formatjs/formatjs/issues"
|
|
24
24
|
},
|
|
25
|
-
"homepage": "https://github.com/formatjs/formatjs#readme"
|
|
25
|
+
"homepage": "https://github.com/formatjs/formatjs#readme",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"tslib": "^2.1.0"
|
|
28
|
+
}
|
|
26
29
|
}
|