@formatjs/fast-memoize 3.0.1 → 3.0.3

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.
Files changed (3) hide show
  1. package/index.d.ts +30 -15
  2. package/index.js +48 -53
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,32 +1,47 @@
1
+ //
2
+ // Main
3
+ //
1
4
  type Func = (...args: any[]) => any;
2
- export interface Cache<K, V> {
3
- create: CacheCreateFunc<K, V>;
5
+ export interface Cache<
6
+ K,
7
+ V
8
+ > {
9
+ create: CacheCreateFunc<K, V>;
4
10
  }
5
- interface CacheCreateFunc<K, V> {
6
- (): DefaultCache<K, V>;
11
+ interface CacheCreateFunc<
12
+ K,
13
+ V
14
+ > {
15
+ (): DefaultCache<K, V>;
7
16
  }
8
- interface DefaultCache<K, V> {
9
- get(key: K): V | undefined;
10
- set(key: K, value: V | undefined): void;
17
+ interface DefaultCache<
18
+ K,
19
+ V
20
+ > {
21
+ get(key: K): V | undefined;
22
+ set(key: K, value: V | undefined): void;
11
23
  }
12
24
  export type Serializer = (args: any[]) => string;
13
25
  export interface Options<F extends Func> {
14
- cache?: Cache<string, ReturnType<F>>;
15
- serializer?: Serializer;
16
- strategy?: MemoizeFunc<F>;
26
+ cache?: Cache<string, ReturnType<F>>;
27
+ serializer?: Serializer;
28
+ strategy?: MemoizeFunc<F>;
17
29
  }
18
30
  export interface ResolvedOptions<F extends Func> {
19
- cache: Cache<string, ReturnType<F>>;
20
- serializer: Serializer;
31
+ cache: Cache<string, ReturnType<F>>;
32
+ serializer: Serializer;
21
33
  }
22
34
  export interface MemoizeFunc<F extends Func> {
23
- (fn: F, options?: Options<F>): F;
35
+ (fn: F, options?: Options<F>): F;
24
36
  }
25
37
  export declare function memoize<F extends Func>(fn: F, options?: Options<F>): F;
26
38
  export type StrategyFn = <F extends Func>(this: unknown, fn: F, cache: DefaultCache<string, ReturnType<F>>, serializer: Serializer, arg: any) => any;
39
+ //
40
+ // API
41
+ //
27
42
  export interface Strategies<F extends Func> {
28
- variadic: MemoizeFunc<F>;
29
- monadic: MemoizeFunc<F>;
43
+ variadic: MemoizeFunc<F>;
44
+ monadic: MemoizeFunc<F>;
30
45
  }
31
46
  export declare const strategies: Strategies<Func>;
32
47
  export {};
package/index.js CHANGED
@@ -1,80 +1,75 @@
1
- //
2
- // Main
3
- //
4
1
  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
- });
2
+ const cache = options && options.cache ? options.cache : cacheDefault;
3
+ const serializer = options && options.serializer ? options.serializer : serializerDefault;
4
+ const strategy = options && options.strategy ? options.strategy : strategyDefault;
5
+ return strategy(fn, {
6
+ cache,
7
+ serializer
8
+ });
12
9
  }
13
10
  //
14
11
  // Strategy
15
12
  //
16
13
  function isPrimitive(value) {
17
- return (value == null || typeof value === 'number' || typeof value === 'boolean'); // || typeof value === "string" 'unsafe' primitive for our needs
14
+ return value == null || typeof value === "number" || typeof value === "boolean";
18
15
  }
19
16
  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;
17
+ const cacheKey = isPrimitive(arg) ? arg : serializer(arg);
18
+ let computedValue = cache.get(cacheKey);
19
+ if (typeof computedValue === "undefined") {
20
+ computedValue = fn.call(this, arg);
21
+ cache.set(cacheKey, computedValue);
22
+ }
23
+ return computedValue;
27
24
  }
28
25
  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;
26
+ const args = Array.prototype.slice.call(arguments, 3);
27
+ const cacheKey = serializer(args);
28
+ let computedValue = cache.get(cacheKey);
29
+ if (typeof computedValue === "undefined") {
30
+ computedValue = fn.apply(this, args);
31
+ cache.set(cacheKey, computedValue);
32
+ }
33
+ return computedValue;
37
34
  }
38
35
  function assemble(fn, context, strategy, cache, serialize) {
39
- return strategy.bind(context, fn, cache, serialize);
36
+ return strategy.bind(context, fn, cache, serialize);
40
37
  }
41
38
  function strategyDefault(fn, options) {
42
- var strategy = fn.length === 1 ? monadic : variadic;
43
- return assemble(fn, this, strategy, options.cache.create(), options.serializer);
39
+ const strategy = fn.length === 1 ? monadic : variadic;
40
+ return assemble(fn, this, strategy, options.cache.create(), options.serializer);
44
41
  }
45
42
  function strategyVariadic(fn, options) {
46
- return assemble(fn, this, variadic, options.cache.create(), options.serializer);
43
+ return assemble(fn, this, variadic, options.cache.create(), options.serializer);
47
44
  }
48
45
  function strategyMonadic(fn, options) {
49
- return assemble(fn, this, monadic, options.cache.create(), options.serializer);
46
+ return assemble(fn, this, monadic, options.cache.create(), options.serializer);
50
47
  }
51
48
  //
52
49
  // Serializer
53
50
  //
54
- var serializerDefault = function () {
55
- return JSON.stringify(arguments);
51
+ const serializerDefault = function() {
52
+ return JSON.stringify(arguments);
56
53
  };
57
54
  //
58
55
  // Cache
59
56
  //
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,
57
+ class ObjectWithoutPrototypeCache {
58
+ cache;
59
+ constructor() {
60
+ this.cache = Object.create(null);
61
+ }
62
+ get(key) {
63
+ return this.cache[key];
64
+ }
65
+ set(key, value) {
66
+ this.cache[key] = value;
67
+ }
68
+ }
69
+ const cacheDefault = { create: function create() {
70
+ return new ObjectWithoutPrototypeCache();
71
+ } };
72
+ export const strategies = {
73
+ variadic: strategyVariadic,
74
+ monadic: strategyMonadic
80
75
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/fast-memoize",
3
3
  "description": "fork of fast-memoize and support esm",
4
- "version": "3.0.1",
4
+ "version": "3.0.3",
5
5
  "license": "MIT",
6
6
  "author": "Long Ho <holevietlong@gmail.com>",
7
7
  "type": "module",