@formatjs/fast-memoize 1.0.0 → 1.2.0
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/LICENSE.md +9 -0
- package/README.md +3 -0
- 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 +5 -2
- package/BUILD +0 -59
- package/index.ts +0 -156
- package/tsconfig.json +0 -5
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 FormatJS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
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":["../../../../../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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ACtB,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":["../../../../../../packages/fast-memoize/index.ts"],"names":[],"mappings":"AAIA,aAAK,IAAI,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK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E,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.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "fork of fast-memoize and support esm",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -22,5 +22,8 @@
|
|
|
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
|
}
|
package/BUILD
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test", "pkg_npm")
|
|
2
|
-
load("//tools:index.bzl", "check_format", "ts_compile")
|
|
3
|
-
load("//tools:jest.bzl", "jest_test")
|
|
4
|
-
|
|
5
|
-
PACKAGE_NAME = "fast-memoize"
|
|
6
|
-
|
|
7
|
-
pkg_npm(
|
|
8
|
-
name = PACKAGE_NAME,
|
|
9
|
-
package_name = "@formatjs/%s" % PACKAGE_NAME,
|
|
10
|
-
srcs = [
|
|
11
|
-
"LICENSE.md",
|
|
12
|
-
"README.md",
|
|
13
|
-
],
|
|
14
|
-
deps = [":dist"],
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
alias(
|
|
18
|
-
name = "publish",
|
|
19
|
-
actual = "%s.publish" % PACKAGE_NAME,
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
alias(
|
|
23
|
-
name = "pack",
|
|
24
|
-
actual = "%s.pack" % PACKAGE_NAME,
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
SRCS = glob([
|
|
28
|
-
"src/*.ts",
|
|
29
|
-
"*.ts",
|
|
30
|
-
])
|
|
31
|
-
|
|
32
|
-
SRC_DEPS = [
|
|
33
|
-
]
|
|
34
|
-
|
|
35
|
-
ts_compile(
|
|
36
|
-
name = "dist",
|
|
37
|
-
package_name = "@formatjs/%s" % PACKAGE_NAME,
|
|
38
|
-
srcs = SRCS,
|
|
39
|
-
deps = SRC_DEPS,
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
generated_file_test(
|
|
43
|
-
name = "tsconfig_json",
|
|
44
|
-
src = "tsconfig.json",
|
|
45
|
-
generated = "//tools:tsconfig.golden.json",
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
check_format(
|
|
49
|
-
name = "prettier",
|
|
50
|
-
srcs = glob(
|
|
51
|
-
[
|
|
52
|
-
"**/*",
|
|
53
|
-
],
|
|
54
|
-
exclude = [
|
|
55
|
-
"CHANGELOG.md",
|
|
56
|
-
"tests/__snapshots__/*",
|
|
57
|
-
],
|
|
58
|
-
),
|
|
59
|
-
)
|
package/index.ts
DELETED
|
@@ -1,156 +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
|
-
(): {
|
|
13
|
-
get(key: K): V
|
|
14
|
-
set(key: K, value: V): void
|
|
15
|
-
has(key: K): boolean
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export type Serializer = (args: any[]) => string
|
|
20
|
-
|
|
21
|
-
export interface Options<F extends Func> {
|
|
22
|
-
cache?: Cache<string, ReturnType<F>>
|
|
23
|
-
serializer?: Serializer
|
|
24
|
-
strategy?: MemoizeFunc
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface MemoizeFunc {
|
|
28
|
-
<F extends Func>(fn: F, options?: Options<F>): F
|
|
29
|
-
}
|
|
30
|
-
const memoize: MemoizeFunc = (fn, options) => {
|
|
31
|
-
const cache = options && options.cache ? options.cache : cacheDefault
|
|
32
|
-
|
|
33
|
-
const serializer =
|
|
34
|
-
options && options.serializer ? options.serializer : serializerDefault
|
|
35
|
-
|
|
36
|
-
const strategy =
|
|
37
|
-
options && options.strategy ? options.strategy : strategyDefault
|
|
38
|
-
|
|
39
|
-
return strategy(fn, {
|
|
40
|
-
cache: cache,
|
|
41
|
-
serializer: serializer,
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
//
|
|
46
|
-
// Strategy
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
function isPrimitive(value: any): boolean {
|
|
50
|
-
return (
|
|
51
|
-
value == null || typeof value === 'number' || typeof value === 'boolean'
|
|
52
|
-
) // || typeof value === "string" 'unsafe' primitive for our needs
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function monadic(fn, cache, serializer, arg) {
|
|
56
|
-
const cacheKey = isPrimitive(arg) ? arg : serializer(arg)
|
|
57
|
-
|
|
58
|
-
let computedValue = cache.get(cacheKey)
|
|
59
|
-
if (typeof computedValue === 'undefined') {
|
|
60
|
-
computedValue = fn.call(this, arg)
|
|
61
|
-
cache.set(cacheKey, computedValue)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return computedValue
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function variadic(fn, cache, serializer) {
|
|
68
|
-
const args = Array.prototype.slice.call(arguments, 3)
|
|
69
|
-
const cacheKey = serializer(args)
|
|
70
|
-
|
|
71
|
-
let computedValue = cache.get(cacheKey)
|
|
72
|
-
if (typeof computedValue === 'undefined') {
|
|
73
|
-
computedValue = fn.apply(this, args)
|
|
74
|
-
cache.set(cacheKey, computedValue)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return computedValue
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function assemble(fn, context, strategy, cache, serialize) {
|
|
81
|
-
return strategy.bind(context, fn, cache, serialize)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function strategyDefault(fn, options) {
|
|
85
|
-
const strategy = fn.length === 1 ? monadic : variadic
|
|
86
|
-
|
|
87
|
-
return assemble(
|
|
88
|
-
fn,
|
|
89
|
-
this,
|
|
90
|
-
strategy,
|
|
91
|
-
options.cache.create(),
|
|
92
|
-
options.serializer
|
|
93
|
-
)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function strategyVariadic(fn, options) {
|
|
97
|
-
return assemble(
|
|
98
|
-
fn,
|
|
99
|
-
this,
|
|
100
|
-
variadic,
|
|
101
|
-
options.cache.create(),
|
|
102
|
-
options.serializer
|
|
103
|
-
)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function strategyMonadic(fn, options) {
|
|
107
|
-
return assemble(fn, this, monadic, options.cache.create(), options.serializer)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
//
|
|
111
|
-
// Serializer
|
|
112
|
-
//
|
|
113
|
-
|
|
114
|
-
function serializerDefault(): string {
|
|
115
|
-
return JSON.stringify(arguments)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
//
|
|
119
|
-
// Cache
|
|
120
|
-
//
|
|
121
|
-
|
|
122
|
-
function ObjectWithoutPrototypeCache() {
|
|
123
|
-
this.cache = Object.create(null) as Record<string, any>
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
ObjectWithoutPrototypeCache.prototype.has = function (key: string) {
|
|
127
|
-
return key in this.cache
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
ObjectWithoutPrototypeCache.prototype.get = function (key: string) {
|
|
131
|
-
return this.cache[key]
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
ObjectWithoutPrototypeCache.prototype.set = function <T>(
|
|
135
|
-
key: string,
|
|
136
|
-
value: T
|
|
137
|
-
): void {
|
|
138
|
-
this.cache[key] = value
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
var cacheDefault = {
|
|
142
|
-
create: function create() {
|
|
143
|
-
return new ObjectWithoutPrototypeCache()
|
|
144
|
-
},
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
//
|
|
148
|
-
// API
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
export const strategies = {
|
|
152
|
-
variadic: strategyVariadic,
|
|
153
|
-
monadic: strategyMonadic,
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export default memoize
|