@formatjs/fast-memoize 1.2.5 → 1.2.6
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 +33 -0
- package/index.d.ts.map +1 -0
- package/index.js +82 -0
- package/lib/index.d.ts +33 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +78 -0
- package/package.json +1 -1
- package/BUILD +0 -62
- package/CHANGELOG.md +0 -50
- package/index.ts +0 -200
- package/tsconfig.json +0 -5
package/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
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["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/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//
|
|
3
|
+
// Main
|
|
4
|
+
//
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.strategies = void 0;
|
|
7
|
+
function memoize(fn, options) {
|
|
8
|
+
var cache = options && options.cache ? options.cache : cacheDefault;
|
|
9
|
+
var serializer = options && options.serializer ? options.serializer : serializerDefault;
|
|
10
|
+
var strategy = options && options.strategy ? options.strategy : strategyDefault;
|
|
11
|
+
return strategy(fn, {
|
|
12
|
+
cache: cache,
|
|
13
|
+
serializer: serializer,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
exports.default = memoize;
|
|
17
|
+
//
|
|
18
|
+
// Strategy
|
|
19
|
+
//
|
|
20
|
+
function isPrimitive(value) {
|
|
21
|
+
return (value == null || typeof value === 'number' || typeof value === 'boolean'); // || typeof value === "string" 'unsafe' primitive for our needs
|
|
22
|
+
}
|
|
23
|
+
function monadic(fn, cache, serializer, arg) {
|
|
24
|
+
var cacheKey = isPrimitive(arg) ? arg : serializer(arg);
|
|
25
|
+
var computedValue = cache.get(cacheKey);
|
|
26
|
+
if (typeof computedValue === 'undefined') {
|
|
27
|
+
computedValue = fn.call(this, arg);
|
|
28
|
+
cache.set(cacheKey, computedValue);
|
|
29
|
+
}
|
|
30
|
+
return computedValue;
|
|
31
|
+
}
|
|
32
|
+
function variadic(fn, cache, serializer) {
|
|
33
|
+
var args = Array.prototype.slice.call(arguments, 3);
|
|
34
|
+
var cacheKey = serializer(args);
|
|
35
|
+
var computedValue = cache.get(cacheKey);
|
|
36
|
+
if (typeof computedValue === 'undefined') {
|
|
37
|
+
computedValue = fn.apply(this, args);
|
|
38
|
+
cache.set(cacheKey, computedValue);
|
|
39
|
+
}
|
|
40
|
+
return computedValue;
|
|
41
|
+
}
|
|
42
|
+
function assemble(fn, context, strategy, cache, serialize) {
|
|
43
|
+
return strategy.bind(context, fn, cache, serialize);
|
|
44
|
+
}
|
|
45
|
+
function strategyDefault(fn, options) {
|
|
46
|
+
var strategy = fn.length === 1 ? monadic : variadic;
|
|
47
|
+
return assemble(fn, this, strategy, options.cache.create(), options.serializer);
|
|
48
|
+
}
|
|
49
|
+
function strategyVariadic(fn, options) {
|
|
50
|
+
return assemble(fn, this, variadic, options.cache.create(), options.serializer);
|
|
51
|
+
}
|
|
52
|
+
function strategyMonadic(fn, options) {
|
|
53
|
+
return assemble(fn, this, monadic, options.cache.create(), options.serializer);
|
|
54
|
+
}
|
|
55
|
+
//
|
|
56
|
+
// Serializer
|
|
57
|
+
//
|
|
58
|
+
var serializerDefault = function () {
|
|
59
|
+
return JSON.stringify(arguments);
|
|
60
|
+
};
|
|
61
|
+
//
|
|
62
|
+
// Cache
|
|
63
|
+
//
|
|
64
|
+
function ObjectWithoutPrototypeCache() {
|
|
65
|
+
this.cache = Object.create(null);
|
|
66
|
+
}
|
|
67
|
+
ObjectWithoutPrototypeCache.prototype.get = function (key) {
|
|
68
|
+
return this.cache[key];
|
|
69
|
+
};
|
|
70
|
+
ObjectWithoutPrototypeCache.prototype.set = function (key, value) {
|
|
71
|
+
this.cache[key] = value;
|
|
72
|
+
};
|
|
73
|
+
var cacheDefault = {
|
|
74
|
+
create: function create() {
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
return new ObjectWithoutPrototypeCache();
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
exports.strategies = {
|
|
80
|
+
variadic: strategyVariadic,
|
|
81
|
+
monadic: strategyMonadic,
|
|
82
|
+
};
|
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":["../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
package/BUILD
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
|
|
2
|
-
load("@aspect_rules_js//npm/private:npm_package.bzl", "npm_package")
|
|
3
|
-
load("//tools:index.bzl", "check_format", "package_json_test", "ts_compile")
|
|
4
|
-
|
|
5
|
-
exports_files([
|
|
6
|
-
"package.json",
|
|
7
|
-
"tsconfig.json",
|
|
8
|
-
])
|
|
9
|
-
|
|
10
|
-
PACKAGE_NAME = "fast-memoize"
|
|
11
|
-
|
|
12
|
-
npm_package(
|
|
13
|
-
name = PACKAGE_NAME,
|
|
14
|
-
srcs = [
|
|
15
|
-
"LICENSE.md",
|
|
16
|
-
"README.md",
|
|
17
|
-
"package.json",
|
|
18
|
-
":dist",
|
|
19
|
-
":dist-esm",
|
|
20
|
-
],
|
|
21
|
-
package = "@formatjs/%s" % PACKAGE_NAME,
|
|
22
|
-
visibility = ["//visibility:public"],
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
SRCS = glob([
|
|
26
|
-
"src/*.ts",
|
|
27
|
-
"*.ts",
|
|
28
|
-
])
|
|
29
|
-
|
|
30
|
-
SRC_DEPS = [
|
|
31
|
-
]
|
|
32
|
-
|
|
33
|
-
ts_compile(
|
|
34
|
-
name = "dist",
|
|
35
|
-
srcs = SRCS,
|
|
36
|
-
package = "@formatjs/%s" % PACKAGE_NAME,
|
|
37
|
-
skip_esm = False,
|
|
38
|
-
deps = SRC_DEPS,
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
write_source_files(
|
|
42
|
-
name = "tsconfig_json",
|
|
43
|
-
files = {"tsconfig.json": "//tools:tsconfig.golden.json"},
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
check_format(
|
|
47
|
-
name = "prettier",
|
|
48
|
-
srcs = glob(
|
|
49
|
-
[
|
|
50
|
-
"**/*",
|
|
51
|
-
],
|
|
52
|
-
exclude = [
|
|
53
|
-
"CHANGELOG.md",
|
|
54
|
-
"tests/__snapshots__/*",
|
|
55
|
-
],
|
|
56
|
-
),
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
package_json_test(
|
|
60
|
-
name = "package_json_test",
|
|
61
|
-
deps = SRC_DEPS,
|
|
62
|
-
)
|
package/CHANGELOG.md
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
# Change Log
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
-
|
|
6
|
-
## [1.2.5](https://github.com/formatjs/formatjs/compare/@formatjs/fast-memoize@1.2.4...@formatjs/fast-memoize@1.2.5) (2022-08-18)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package @formatjs/fast-memoize
|
|
9
|
-
|
|
10
|
-
## [1.2.4](https://github.com/formatjs/formatjs/compare/@formatjs/fast-memoize@1.2.3...@formatjs/fast-memoize@1.2.4) (2022-06-06)
|
|
11
|
-
|
|
12
|
-
**Note:** Version bump only for package @formatjs/fast-memoize
|
|
13
|
-
|
|
14
|
-
## [1.2.3](https://github.com/formatjs/formatjs/compare/@formatjs/fast-memoize@1.2.2...@formatjs/fast-memoize@1.2.3) (2022-05-19)
|
|
15
|
-
|
|
16
|
-
**Note:** Version bump only for package @formatjs/fast-memoize
|
|
17
|
-
|
|
18
|
-
## [1.2.2](https://github.com/formatjs/formatjs/compare/@formatjs/fast-memoize@1.2.1...@formatjs/fast-memoize@1.2.2) (2022-05-19)
|
|
19
|
-
|
|
20
|
-
### Bug Fixes
|
|
21
|
-
|
|
22
|
-
* **react-intl:** fix type issue with react18, fix [#3550](https://github.com/formatjs/formatjs/issues/3550) ([2567b93](https://github.com/formatjs/formatjs/commit/2567b932c5d18b097a43842563046c20ce0c49f1))
|
|
23
|
-
|
|
24
|
-
## [1.2.1](https://github.com/formatjs/formatjs/compare/@formatjs/fast-memoize@1.2.0...@formatjs/fast-memoize@1.2.1) (2021-12-20)
|
|
25
|
-
|
|
26
|
-
### Bug Fixes
|
|
27
|
-
|
|
28
|
-
* **@formatjs/fast-memoize:** fixed fast-memoize package license information ([#3301](https://github.com/formatjs/formatjs/issues/3301)) ([ff7ea83](https://github.com/formatjs/formatjs/commit/ff7ea837cabf7d82b7e0d808c753557bec1a63b2))
|
|
29
|
-
|
|
30
|
-
# [1.2.0](https://github.com/formatjs/formatjs/compare/@formatjs/fast-memoize@1.1.2...@formatjs/fast-memoize@1.2.0) (2021-08-15)
|
|
31
|
-
|
|
32
|
-
### Features
|
|
33
|
-
|
|
34
|
-
* **@formatjs/fast-memoize:** remove unused Cache.has ([#3102](https://github.com/formatjs/formatjs/issues/3102)) ([5e9a425](https://github.com/formatjs/formatjs/commit/5e9a425519fd2b2473172687fccb58a6979ec81e))
|
|
35
|
-
|
|
36
|
-
## [1.1.2](https://github.com/formatjs/formatjs/compare/@formatjs/fast-memoize@1.1.1...@formatjs/fast-memoize@1.1.2) (2021-08-06)
|
|
37
|
-
|
|
38
|
-
**Note:** Version bump only for package @formatjs/fast-memoize
|
|
39
|
-
|
|
40
|
-
## [1.1.1](https://github.com/formatjs/formatjs/compare/@formatjs/fast-memoize@1.1.0...@formatjs/fast-memoize@1.1.1) (2021-04-26)
|
|
41
|
-
|
|
42
|
-
### Bug Fixes
|
|
43
|
-
|
|
44
|
-
* **@formatjs/fast-memoize:** expose ESM module ([c68e850](https://github.com/formatjs/formatjs/commit/c68e8508956ec6e3f13e2f0aed0419fcd2c453ce))
|
|
45
|
-
|
|
46
|
-
# 1.1.0 (2021-04-26)
|
|
47
|
-
|
|
48
|
-
### Features
|
|
49
|
-
|
|
50
|
-
* **@formatjs/fast-memoize:** publish our fork ([18026f0](https://github.com/formatjs/formatjs/commit/18026f0a5f986a385efd3793ce9190024a3f903c))
|
package/index.ts
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Main
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
type Func = (...args: any[]) => any
|
|
6
|
-
|
|
7
|
-
export interface Cache<K, V> {
|
|
8
|
-
create: CacheCreateFunc<K, V>
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
interface CacheCreateFunc<K, V> {
|
|
12
|
-
(): DefaultCache<K, V>
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface DefaultCache<K, V> {
|
|
16
|
-
get(key: K): V
|
|
17
|
-
set(key: K, value: V): void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export type Serializer = (args: any[]) => string
|
|
21
|
-
|
|
22
|
-
export interface Options<F extends Func> {
|
|
23
|
-
cache?: Cache<string, ReturnType<F>>
|
|
24
|
-
serializer?: Serializer
|
|
25
|
-
strategy?: MemoizeFunc<F>
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface ResolvedOptions<F extends Func> {
|
|
29
|
-
cache: Cache<string, ReturnType<F>>
|
|
30
|
-
serializer: Serializer
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface MemoizeFunc<F extends Func> {
|
|
34
|
-
(fn: F, options?: Options<F>): F
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export default function memoize<F extends Func>(fn: F, options?: Options<F>) {
|
|
38
|
-
const cache = options && options.cache ? options.cache : cacheDefault
|
|
39
|
-
|
|
40
|
-
const serializer =
|
|
41
|
-
options && options.serializer ? options.serializer : serializerDefault
|
|
42
|
-
|
|
43
|
-
const strategy =
|
|
44
|
-
options && options.strategy ? options.strategy : strategyDefault
|
|
45
|
-
|
|
46
|
-
return strategy(fn, {
|
|
47
|
-
cache,
|
|
48
|
-
serializer,
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
//
|
|
53
|
-
// Strategy
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
function isPrimitive(value: any): boolean {
|
|
57
|
-
return (
|
|
58
|
-
value == null || typeof value === 'number' || typeof value === 'boolean'
|
|
59
|
-
) // || typeof value === "string" 'unsafe' primitive for our needs
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type StrategyFn = <F extends Func>(
|
|
63
|
-
this: unknown,
|
|
64
|
-
fn: F,
|
|
65
|
-
cache: DefaultCache<string, ReturnType<F>>,
|
|
66
|
-
serializer: Serializer,
|
|
67
|
-
arg: any
|
|
68
|
-
) => any
|
|
69
|
-
|
|
70
|
-
function monadic<F extends Func>(
|
|
71
|
-
this: unknown,
|
|
72
|
-
fn: F,
|
|
73
|
-
cache: DefaultCache<string, ReturnType<F>>,
|
|
74
|
-
serializer: Serializer,
|
|
75
|
-
arg: any
|
|
76
|
-
) {
|
|
77
|
-
const cacheKey = isPrimitive(arg) ? arg : serializer(arg)
|
|
78
|
-
|
|
79
|
-
let computedValue = cache.get(cacheKey)
|
|
80
|
-
if (typeof computedValue === 'undefined') {
|
|
81
|
-
computedValue = fn.call(this, arg)
|
|
82
|
-
cache.set(cacheKey, computedValue)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return computedValue
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function variadic<F extends Func>(
|
|
89
|
-
this: unknown,
|
|
90
|
-
fn: F,
|
|
91
|
-
cache: DefaultCache<string, ReturnType<F>>,
|
|
92
|
-
serializer: Serializer
|
|
93
|
-
) {
|
|
94
|
-
const args = Array.prototype.slice.call(arguments, 3)
|
|
95
|
-
const cacheKey = serializer(args)
|
|
96
|
-
|
|
97
|
-
let computedValue = cache.get(cacheKey)
|
|
98
|
-
if (typeof computedValue === 'undefined') {
|
|
99
|
-
computedValue = fn.apply(this, args)
|
|
100
|
-
cache.set(cacheKey, computedValue)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return computedValue
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function assemble<F extends Func>(
|
|
107
|
-
fn: F,
|
|
108
|
-
context: unknown,
|
|
109
|
-
strategy: StrategyFn,
|
|
110
|
-
cache: DefaultCache<string, ReturnType<F>>,
|
|
111
|
-
serialize: Serializer
|
|
112
|
-
) {
|
|
113
|
-
return strategy.bind(context, fn, cache, serialize)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function strategyDefault<F extends Func>(
|
|
117
|
-
this: unknown,
|
|
118
|
-
fn: F,
|
|
119
|
-
options: ResolvedOptions<F>
|
|
120
|
-
) {
|
|
121
|
-
const strategy = fn.length === 1 ? monadic : variadic
|
|
122
|
-
|
|
123
|
-
return assemble(
|
|
124
|
-
fn,
|
|
125
|
-
this,
|
|
126
|
-
strategy,
|
|
127
|
-
options.cache.create(),
|
|
128
|
-
options.serializer
|
|
129
|
-
)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function strategyVariadic<F extends Func>(
|
|
133
|
-
this: unknown,
|
|
134
|
-
fn: F,
|
|
135
|
-
options: ResolvedOptions<F>
|
|
136
|
-
) {
|
|
137
|
-
return assemble(
|
|
138
|
-
fn,
|
|
139
|
-
this,
|
|
140
|
-
variadic,
|
|
141
|
-
options.cache.create(),
|
|
142
|
-
options.serializer
|
|
143
|
-
)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function strategyMonadic<F extends Func>(
|
|
147
|
-
this: unknown,
|
|
148
|
-
fn: F,
|
|
149
|
-
options: ResolvedOptions<F>
|
|
150
|
-
) {
|
|
151
|
-
return assemble(fn, this, monadic, options.cache.create(), options.serializer)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
//
|
|
155
|
-
// Serializer
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
const serializerDefault: Serializer = function (): string {
|
|
159
|
-
return JSON.stringify(arguments)
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
//
|
|
163
|
-
// Cache
|
|
164
|
-
//
|
|
165
|
-
|
|
166
|
-
function ObjectWithoutPrototypeCache(this: any) {
|
|
167
|
-
this.cache = Object.create(null) as Record<string, any>
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
ObjectWithoutPrototypeCache.prototype.get = function (key: string) {
|
|
171
|
-
return this.cache[key]
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
ObjectWithoutPrototypeCache.prototype.set = function <T>(
|
|
175
|
-
key: string,
|
|
176
|
-
value: T
|
|
177
|
-
): void {
|
|
178
|
-
this.cache[key] = value
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const cacheDefault: Cache<any, any> = {
|
|
182
|
-
create: function create() {
|
|
183
|
-
// @ts-ignore
|
|
184
|
-
return new ObjectWithoutPrototypeCache()
|
|
185
|
-
},
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
//
|
|
189
|
-
// API
|
|
190
|
-
//
|
|
191
|
-
|
|
192
|
-
export interface Strategies<F extends Func> {
|
|
193
|
-
variadic: MemoizeFunc<F>
|
|
194
|
-
monadic: MemoizeFunc<F>
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export const strategies: Strategies<Func> = {
|
|
198
|
-
variadic: strategyVariadic as MemoizeFunc<Func>,
|
|
199
|
-
monadic: strategyMonadic as MemoizeFunc<Func>,
|
|
200
|
-
}
|