@common.js/formatjs__fast-memoize 3.1.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/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 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
@@ -0,0 +1,5 @@
1
+ # @common.js/formatjs__fast-memoize
2
+
3
+ The [@formatjs/fast-memoize](https://www.npmjs.com/package/@formatjs/fast-memoize) package exported as CommonJS modules.
4
+
5
+ Exported from [@formatjs/fast-memoize@3.1.6](https://www.npmjs.com/package/@formatjs/fast-memoize/v/3.1.6) using https://github.com/etienne-martin/common.js.
package/index.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ //#region packages/fast-memoize/index.d.ts
2
+ type Func = (...args: any[]) => any;
3
+ interface Cache<K, V> {
4
+ create: CacheCreateFunc<K, V>;
5
+ }
6
+ interface CacheCreateFunc<K, V> {
7
+ (): DefaultCache<K, V>;
8
+ }
9
+ interface DefaultCache<K, V> {
10
+ get(key: K): V | undefined;
11
+ set(key: K, value: V | undefined): void;
12
+ }
13
+ type Serializer = (args: any[]) => string;
14
+ interface Options<F extends Func> {
15
+ cache?: Cache<string, ReturnType<F>>;
16
+ serializer?: Serializer;
17
+ strategy?: MemoizeFunc<F>;
18
+ }
19
+ interface ResolvedOptions<F extends Func> {
20
+ cache: Cache<string, ReturnType<F>>;
21
+ serializer: Serializer;
22
+ }
23
+ interface MemoizeFunc<F extends Func> {
24
+ (fn: F, options?: Options<F>): F;
25
+ }
26
+ declare function memoize<F extends Func>(fn: F, options?: Options<F>): F;
27
+ type StrategyFn = <F extends Func>(this: unknown, fn: F, cache: DefaultCache<string, ReturnType<F>>, serializer: Serializer, arg: any) => any;
28
+ interface Strategies<F extends Func> {
29
+ variadic: MemoizeFunc<F>;
30
+ monadic: MemoizeFunc<F>;
31
+ }
32
+ declare const strategies: Strategies<Func>;
33
+ //#endregion
34
+ export { Cache, MemoizeFunc, Options, ResolvedOptions, Serializer, Strategies, StrategyFn, memoize, strategies };
35
+ //# sourceMappingURL=index.d.ts.map
package/index.js ADDED
@@ -0,0 +1,116 @@
1
+ //#region packages/fast-memoize/index.ts
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ function _export(target, all) {
7
+ for(var name in all)Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ memoize: function() {
14
+ return memoize;
15
+ },
16
+ strategies: function() {
17
+ return strategies;
18
+ }
19
+ });
20
+ function _classCallCheck(instance, Constructor) {
21
+ if (!(instance instanceof Constructor)) {
22
+ throw new TypeError("Cannot call a class as a function");
23
+ }
24
+ }
25
+ function _defineProperties(target, props) {
26
+ for(var i = 0; i < props.length; i++){
27
+ var descriptor = props[i];
28
+ descriptor.enumerable = descriptor.enumerable || false;
29
+ descriptor.configurable = true;
30
+ if ("value" in descriptor) descriptor.writable = true;
31
+ Object.defineProperty(target, descriptor.key, descriptor);
32
+ }
33
+ }
34
+ function _createClass(Constructor, protoProps, staticProps) {
35
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
36
+ if (staticProps) _defineProperties(Constructor, staticProps);
37
+ return Constructor;
38
+ }
39
+ function memoize(fn, options) {
40
+ var cache = options && options.cache ? options.cache : cacheDefault;
41
+ var serializer = options && options.serializer ? options.serializer : serializerDefault;
42
+ return (options && options.strategy ? options.strategy : strategyDefault)(fn, {
43
+ cache: cache,
44
+ serializer: serializer
45
+ });
46
+ }
47
+ function isPrimitive(value) {
48
+ return value == null || typeof value === "number" || typeof value === "boolean";
49
+ }
50
+ function monadic(fn, cache, serializer, arg) {
51
+ var cacheKey = isPrimitive(arg) ? arg : serializer(arg);
52
+ var computedValue = cache.get(cacheKey);
53
+ if (typeof computedValue === "undefined") {
54
+ computedValue = fn.call(this, arg);
55
+ cache.set(cacheKey, computedValue);
56
+ }
57
+ return computedValue;
58
+ }
59
+ function variadic(fn, cache, serializer) {
60
+ var args = Array.prototype.slice.call(arguments, 3);
61
+ var cacheKey = serializer(args);
62
+ var computedValue = cache.get(cacheKey);
63
+ if (typeof computedValue === "undefined") {
64
+ computedValue = fn.apply(this, args);
65
+ cache.set(cacheKey, computedValue);
66
+ }
67
+ return computedValue;
68
+ }
69
+ function assemble(fn, context, strategy, cache, serialize) {
70
+ return strategy.bind(context, fn, cache, serialize);
71
+ }
72
+ function strategyDefault(fn, options) {
73
+ var strategy = fn.length === 1 ? monadic : variadic;
74
+ return assemble(fn, this, strategy, options.cache.create(), options.serializer);
75
+ }
76
+ function strategyVariadic(fn, options) {
77
+ return assemble(fn, this, variadic, options.cache.create(), options.serializer);
78
+ }
79
+ function strategyMonadic(fn, options) {
80
+ return assemble(fn, this, monadic, options.cache.create(), options.serializer);
81
+ }
82
+ var serializerDefault = function serializerDefault() {
83
+ return JSON.stringify(arguments);
84
+ };
85
+ var ObjectWithoutPrototypeCache = /*#__PURE__*/ function() {
86
+ "use strict";
87
+ function ObjectWithoutPrototypeCache() {
88
+ _classCallCheck(this, ObjectWithoutPrototypeCache);
89
+ this.cache = Object.create(null);
90
+ }
91
+ _createClass(ObjectWithoutPrototypeCache, [
92
+ {
93
+ key: "get",
94
+ value: function get(key) {
95
+ return this.cache[key];
96
+ }
97
+ },
98
+ {
99
+ key: "set",
100
+ value: function set(key, value) {
101
+ this.cache[key] = value;
102
+ }
103
+ }
104
+ ]);
105
+ return ObjectWithoutPrototypeCache;
106
+ }();
107
+ var cacheDefault = {
108
+ create: function create() {
109
+ return new ObjectWithoutPrototypeCache();
110
+ }
111
+ };
112
+ var strategies = {
113
+ variadic: strategyVariadic,
114
+ monadic: strategyMonadic
115
+ };
116
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../index.ts"],"sourcesContent":["//\n// Main\n//\n\ntype Func = (...args: any[]) => any\n\nexport interface Cache<K, V> {\n create: CacheCreateFunc<K, V>\n}\n\ninterface CacheCreateFunc<K, V> {\n (): DefaultCache<K, V>\n}\n\ninterface DefaultCache<K, V> {\n get(key: K): V | undefined\n set(key: K, value: V | undefined): void\n}\n\nexport type Serializer = (args: any[]) => string\n\nexport interface Options<F extends Func> {\n cache?: Cache<string, ReturnType<F>>\n serializer?: Serializer\n strategy?: MemoizeFunc<F>\n}\n\nexport interface ResolvedOptions<F extends Func> {\n cache: Cache<string, ReturnType<F>>\n serializer: Serializer\n}\n\nexport interface MemoizeFunc<F extends Func> {\n (fn: F, options?: Options<F>): F\n}\n\nexport function memoize<F extends Func>(fn: F, options?: Options<F>): F {\n const cache = options && options.cache ? options.cache : cacheDefault\n\n const serializer =\n options && options.serializer ? options.serializer : serializerDefault\n\n const strategy =\n options && options.strategy ? options.strategy : strategyDefault\n\n return strategy(fn, {\n cache,\n serializer,\n })\n}\n\n//\n// Strategy\n//\n\nfunction isPrimitive(value: any): boolean {\n return (\n value == null || typeof value === 'number' || typeof value === 'boolean'\n ) // || typeof value === \"string\" 'unsafe' primitive for our needs\n}\n\nexport type StrategyFn = <F extends Func>(\n this: unknown,\n fn: F,\n cache: DefaultCache<string, ReturnType<F>>,\n serializer: Serializer,\n arg: any\n) => any\n\nfunction monadic<F extends Func>(\n this: unknown,\n fn: F,\n cache: DefaultCache<string, ReturnType<F>>,\n serializer: Serializer,\n arg: any\n) {\n const cacheKey = isPrimitive(arg) ? arg : serializer(arg)\n\n let computedValue = cache.get(cacheKey)\n if (typeof computedValue === 'undefined') {\n computedValue = fn.call(this, arg)\n cache.set(cacheKey, computedValue)\n }\n\n return computedValue\n}\n\nfunction variadic<F extends Func>(\n this: unknown,\n fn: F,\n cache: DefaultCache<string, ReturnType<F>>,\n serializer: Serializer\n) {\n const args = Array.prototype.slice.call(arguments, 3)\n const cacheKey = serializer(args)\n\n let computedValue = cache.get(cacheKey)\n if (typeof computedValue === 'undefined') {\n computedValue = fn.apply(this, args)\n cache.set(cacheKey, computedValue)\n }\n\n return computedValue\n}\n\nfunction assemble<F extends Func>(\n fn: F,\n context: unknown,\n strategy: StrategyFn,\n cache: DefaultCache<string, ReturnType<F>>,\n serialize: Serializer\n): F {\n return strategy.bind(context, fn, cache, serialize) as F\n}\n\nfunction strategyDefault<F extends Func>(\n this: unknown,\n fn: F,\n options: ResolvedOptions<F>\n) {\n const strategy = fn.length === 1 ? monadic : variadic\n\n return assemble(\n fn,\n this,\n strategy,\n options.cache.create(),\n options.serializer\n )\n}\n\nfunction strategyVariadic<F extends Func>(\n this: unknown,\n fn: F,\n options: ResolvedOptions<F>\n) {\n return assemble(\n fn,\n this,\n variadic,\n options.cache.create(),\n options.serializer\n )\n}\n\nfunction strategyMonadic<F extends Func>(\n this: unknown,\n fn: F,\n options: ResolvedOptions<F>\n) {\n return assemble(fn, this, monadic, options.cache.create(), options.serializer)\n}\n\n//\n// Serializer\n//\n\nconst serializerDefault: Serializer = function (): string {\n return JSON.stringify(arguments)\n}\n\n//\n// Cache\n//\n\nclass ObjectWithoutPrototypeCache {\n private cache: Record<string, any>\n\n constructor() {\n this.cache = Object.create(null) as Record<string, any>\n }\n\n get(key: string) {\n return this.cache[key]\n }\n\n set<T>(key: string, value: T): void {\n this.cache[key] = value\n }\n}\n\nconst cacheDefault: Cache<any, any> = {\n create: function create() {\n return new ObjectWithoutPrototypeCache()\n },\n}\n\n//\n// API\n//\n\nexport interface Strategies<F extends Func> {\n variadic: MemoizeFunc<F>\n monadic: MemoizeFunc<F>\n}\n\nexport const strategies: Strategies<Func> = {\n variadic: strategyVariadic as MemoizeFunc<Func>,\n monadic: strategyMonadic as MemoizeFunc<Func>,\n}\n"],"mappings":";AAoCA,SAAgB,QAAwB,IAAO,SAAyB;CACtE,MAAM,QAAQ,WAAW,QAAQ,QAAQ,QAAQ,QAAQ;CAEzD,MAAM,aACJ,WAAW,QAAQ,aAAa,QAAQ,aAAa;CAKvD,QAFE,WAAW,QAAQ,WAAW,QAAQ,WAAW,iBAEnC,IAAI;EAClB;EACA;CACF,CAAC;AACH;AAMA,SAAS,YAAY,OAAqB;CACxC,OACE,SAAS,QAAQ,OAAO,UAAU,YAAY,OAAO,UAAU;AAEnE;AAUA,SAAS,QAEP,IACA,OACA,YACA,KACA;CACA,MAAM,WAAW,YAAY,GAAG,IAAI,MAAM,WAAW,GAAG;CAExD,IAAI,gBAAgB,MAAM,IAAI,QAAQ;CACtC,IAAI,OAAO,kBAAkB,aAAa;EACxC,gBAAgB,GAAG,KAAK,MAAM,GAAG;EACjC,MAAM,IAAI,UAAU,aAAa;CACnC;CAEA,OAAO;AACT;AAEA,SAAS,SAEP,IACA,OACA,YACA;CACA,MAAM,OAAO,MAAM,UAAU,MAAM,KAAK,WAAW,CAAC;CACpD,MAAM,WAAW,WAAW,IAAI;CAEhC,IAAI,gBAAgB,MAAM,IAAI,QAAQ;CACtC,IAAI,OAAO,kBAAkB,aAAa;EACxC,gBAAgB,GAAG,MAAM,MAAM,IAAI;EACnC,MAAM,IAAI,UAAU,aAAa;CACnC;CAEA,OAAO;AACT;AAEA,SAAS,SACP,IACA,SACA,UACA,OACA,WACG;CACH,OAAO,SAAS,KAAK,SAAS,IAAI,OAAO,SAAS;AACpD;AAEA,SAAS,gBAEP,IACA,SACA;CACA,MAAM,WAAW,GAAG,WAAW,IAAI,UAAU;CAE7C,OAAO,SACL,IACA,MACA,UACA,QAAQ,MAAM,OAAO,GACrB,QAAQ,UACV;AACF;AAEA,SAAS,iBAEP,IACA,SACA;CACA,OAAO,SACL,IACA,MACA,UACA,QAAQ,MAAM,OAAO,GACrB,QAAQ,UACV;AACF;AAEA,SAAS,gBAEP,IACA,SACA;CACA,OAAO,SAAS,IAAI,MAAM,SAAS,QAAQ,MAAM,OAAO,GAAG,QAAQ,UAAU;AAC/E;AAMA,MAAM,oBAAgC,WAAoB;CACxD,OAAO,KAAK,UAAU,SAAS;AACjC;AAMA,IAAM,8BAAN,MAAkC;CAGhC,cAAc;EACZ,KAAK,QAAQ,OAAO,OAAO,IAAI;CACjC;CAEA,IAAI,KAAa;EACf,OAAO,KAAK,MAAM;CACpB;CAEA,IAAO,KAAa,OAAgB;EAClC,KAAK,MAAM,OAAO;CACpB;AACF;AAEA,MAAM,eAAgC,EACpC,QAAQ,SAAS,SAAS;CACxB,OAAO,IAAI,4BAA4B;AACzC,EACF;AAWA,MAAa,aAA+B;CAC1C,UAAU;CACV,SAAS;AACX"}
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@common.js/formatjs__fast-memoize",
3
+ "version": "3.1.6",
4
+ "description": "@formatjs/fast-memoize package exported as CommonJS modules",
5
+ "homepage": "https://github.com/etienne-martin/common.js#readme",
6
+ "bugs": "https://github.com/formatjs/formatjs/issues",
7
+ "license": "MIT",
8
+ "repository": "etienne-martin/common.js",
9
+ "type": "commonjs",
10
+ "sideEffects": false,
11
+ "types": "index.d.ts",
12
+ "dependencies": {},
13
+ "scripts": {},
14
+ "main": "./index.js"
15
+ }