@formatjs/fast-memoize 2.2.7 → 3.0.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 +1 -1
- package/index.js +2 -6
- package/package.json +7 -3
- package/lib/index.d.ts +0 -32
- package/lib/index.js +0 -80
package/index.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export interface ResolvedOptions<F extends Func> {
|
|
|
22
22
|
export interface MemoizeFunc<F extends Func> {
|
|
23
23
|
(fn: F, options?: Options<F>): F;
|
|
24
24
|
}
|
|
25
|
-
export declare function memoize<F extends Func>(fn: F, options?: Options<F>): F
|
|
25
|
+
export declare function memoize<F extends Func>(fn: F, options?: Options<F>): F;
|
|
26
26
|
export type StrategyFn = <F extends Func>(this: unknown, fn: F, cache: DefaultCache<string, ReturnType<F>>, serializer: Serializer, arg: any) => any;
|
|
27
27
|
export interface Strategies<F extends Func> {
|
|
28
28
|
variadic: MemoizeFunc<F>;
|
package/index.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
//
|
|
3
2
|
// Main
|
|
4
3
|
//
|
|
5
|
-
|
|
6
|
-
exports.strategies = void 0;
|
|
7
|
-
exports.memoize = memoize;
|
|
8
|
-
function memoize(fn, options) {
|
|
4
|
+
export function memoize(fn, options) {
|
|
9
5
|
var cache = options && options.cache ? options.cache : cacheDefault;
|
|
10
6
|
var serializer = options && options.serializer ? options.serializer : serializerDefault;
|
|
11
7
|
var strategy = options && options.strategy ? options.strategy : strategyDefault;
|
|
@@ -78,7 +74,7 @@ var cacheDefault = {
|
|
|
78
74
|
return new ObjectWithoutPrototypeCache();
|
|
79
75
|
},
|
|
80
76
|
};
|
|
81
|
-
|
|
77
|
+
export var strategies = {
|
|
82
78
|
variadic: strategyVariadic,
|
|
83
79
|
monadic: strategyMonadic,
|
|
84
80
|
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/fast-memoize",
|
|
3
3
|
"description": "fork of fast-memoize and support esm",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Long Ho <holevietlong@gmail.com>",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"types": "index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./index.js"
|
|
12
|
+
},
|
|
7
13
|
"dependencies": {
|
|
8
14
|
"tslib": "^2.8.0"
|
|
9
15
|
},
|
|
@@ -15,7 +21,5 @@
|
|
|
15
21
|
"intl",
|
|
16
22
|
"memoize"
|
|
17
23
|
],
|
|
18
|
-
"main": "index.js",
|
|
19
|
-
"module": "lib/index.js",
|
|
20
24
|
"repository": "formatjs/formatjs.git"
|
|
21
25
|
}
|
package/lib/index.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
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 | undefined;
|
|
10
|
-
set(key: K, value: V | undefined): void;
|
|
11
|
-
}
|
|
12
|
-
export 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 declare function memoize<F extends Func>(fn: F, options?: Options<F>): F | ((arg: any) => any);
|
|
26
|
-
export 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 {};
|
package/lib/index.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Main
|
|
3
|
-
//
|
|
4
|
-
export 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
|
-
var ObjectWithoutPrototypeCache = /** @class */ (function () {
|
|
61
|
-
function ObjectWithoutPrototypeCache() {
|
|
62
|
-
this.cache = Object.create(null);
|
|
63
|
-
}
|
|
64
|
-
ObjectWithoutPrototypeCache.prototype.get = function (key) {
|
|
65
|
-
return this.cache[key];
|
|
66
|
-
};
|
|
67
|
-
ObjectWithoutPrototypeCache.prototype.set = function (key, value) {
|
|
68
|
-
this.cache[key] = value;
|
|
69
|
-
};
|
|
70
|
-
return ObjectWithoutPrototypeCache;
|
|
71
|
-
}());
|
|
72
|
-
var cacheDefault = {
|
|
73
|
-
create: function create() {
|
|
74
|
-
return new ObjectWithoutPrototypeCache();
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
export var strategies = {
|
|
78
|
-
variadic: strategyVariadic,
|
|
79
|
-
monadic: strategyMonadic,
|
|
80
|
-
};
|