@formatjs/fast-memoize
Advanced tools
+30
-15
@@ -0,32 +1,47 @@ | ||
| // | ||
| // Main | ||
| // | ||
| type Func = (...args: any[]) => any; | ||
| export interface Cache<K, V> { | ||
| create: CacheCreateFunc<K, V>; | ||
| export interface Cache< | ||
| K, | ||
| V | ||
| > { | ||
| create: CacheCreateFunc<K, V>; | ||
| } | ||
| interface CacheCreateFunc<K, V> { | ||
| (): DefaultCache<K, V>; | ||
| interface CacheCreateFunc< | ||
| K, | ||
| V | ||
| > { | ||
| (): DefaultCache<K, V>; | ||
| } | ||
| interface DefaultCache<K, V> { | ||
| get(key: K): V | undefined; | ||
| set(key: K, value: V | undefined): void; | ||
| interface DefaultCache< | ||
| K, | ||
| V | ||
| > { | ||
| get(key: K): V | undefined; | ||
| set(key: K, value: V | undefined): void; | ||
| } | ||
| export type Serializer = (args: any[]) => string; | ||
| export interface Options<F extends Func> { | ||
| cache?: Cache<string, ReturnType<F>>; | ||
| serializer?: Serializer; | ||
| strategy?: MemoizeFunc<F>; | ||
| cache?: Cache<string, ReturnType<F>>; | ||
| serializer?: Serializer; | ||
| strategy?: MemoizeFunc<F>; | ||
| } | ||
| export interface ResolvedOptions<F extends Func> { | ||
| cache: Cache<string, ReturnType<F>>; | ||
| serializer: Serializer; | ||
| cache: Cache<string, ReturnType<F>>; | ||
| serializer: Serializer; | ||
| } | ||
| export interface MemoizeFunc<F extends Func> { | ||
| (fn: F, options?: Options<F>): F; | ||
| (fn: F, options?: Options<F>): F; | ||
| } | ||
| export declare function memoize<F extends Func>(fn: F, options?: Options<F>): F; | ||
| export type StrategyFn = <F extends Func>(this: unknown, fn: F, cache: DefaultCache<string, ReturnType<F>>, serializer: Serializer, arg: any) => any; | ||
| // | ||
| // API | ||
| // | ||
| export interface Strategies<F extends Func> { | ||
| variadic: MemoizeFunc<F>; | ||
| monadic: MemoizeFunc<F>; | ||
| variadic: MemoizeFunc<F>; | ||
| monadic: MemoizeFunc<F>; | ||
| } | ||
| export declare const strategies: Strategies<Func>; | ||
| export {}; |
+48
-53
@@ -1,12 +0,9 @@ | ||
| // | ||
| // Main | ||
| // | ||
| export function memoize(fn, options) { | ||
| var cache = options && options.cache ? options.cache : cacheDefault; | ||
| var serializer = options && options.serializer ? options.serializer : serializerDefault; | ||
| var strategy = options && options.strategy ? options.strategy : strategyDefault; | ||
| return strategy(fn, { | ||
| cache: cache, | ||
| serializer: serializer, | ||
| }); | ||
| const cache = options && options.cache ? options.cache : cacheDefault; | ||
| const serializer = options && options.serializer ? options.serializer : serializerDefault; | ||
| const strategy = options && options.strategy ? options.strategy : strategyDefault; | ||
| return strategy(fn, { | ||
| cache, | ||
| serializer | ||
| }); | ||
| } | ||
@@ -17,35 +14,35 @@ // | ||
| function isPrimitive(value) { | ||
| return (value == null || typeof value === 'number' || typeof value === 'boolean'); // || typeof value === "string" 'unsafe' primitive for our needs | ||
| return value == null || typeof value === "number" || typeof value === "boolean"; | ||
| } | ||
| function monadic(fn, cache, serializer, arg) { | ||
| var cacheKey = isPrimitive(arg) ? arg : serializer(arg); | ||
| var computedValue = cache.get(cacheKey); | ||
| if (typeof computedValue === 'undefined') { | ||
| computedValue = fn.call(this, arg); | ||
| cache.set(cacheKey, computedValue); | ||
| } | ||
| return computedValue; | ||
| const cacheKey = isPrimitive(arg) ? arg : serializer(arg); | ||
| let computedValue = cache.get(cacheKey); | ||
| if (typeof computedValue === "undefined") { | ||
| computedValue = fn.call(this, arg); | ||
| cache.set(cacheKey, computedValue); | ||
| } | ||
| return computedValue; | ||
| } | ||
| function variadic(fn, cache, serializer) { | ||
| var args = Array.prototype.slice.call(arguments, 3); | ||
| var cacheKey = serializer(args); | ||
| var computedValue = cache.get(cacheKey); | ||
| if (typeof computedValue === 'undefined') { | ||
| computedValue = fn.apply(this, args); | ||
| cache.set(cacheKey, computedValue); | ||
| } | ||
| return computedValue; | ||
| const args = Array.prototype.slice.call(arguments, 3); | ||
| const cacheKey = serializer(args); | ||
| let computedValue = cache.get(cacheKey); | ||
| if (typeof computedValue === "undefined") { | ||
| computedValue = fn.apply(this, args); | ||
| cache.set(cacheKey, computedValue); | ||
| } | ||
| return computedValue; | ||
| } | ||
| function assemble(fn, context, strategy, cache, serialize) { | ||
| return strategy.bind(context, fn, cache, serialize); | ||
| return strategy.bind(context, fn, cache, serialize); | ||
| } | ||
| function strategyDefault(fn, options) { | ||
| var strategy = fn.length === 1 ? monadic : variadic; | ||
| return assemble(fn, this, strategy, options.cache.create(), options.serializer); | ||
| const strategy = fn.length === 1 ? monadic : variadic; | ||
| return assemble(fn, this, strategy, options.cache.create(), options.serializer); | ||
| } | ||
| function strategyVariadic(fn, options) { | ||
| return assemble(fn, this, variadic, options.cache.create(), options.serializer); | ||
| return assemble(fn, this, variadic, options.cache.create(), options.serializer); | ||
| } | ||
| function strategyMonadic(fn, options) { | ||
| return assemble(fn, this, monadic, options.cache.create(), options.serializer); | ||
| return assemble(fn, this, monadic, options.cache.create(), options.serializer); | ||
| } | ||
@@ -55,4 +52,4 @@ // | ||
| // | ||
| var serializerDefault = function () { | ||
| return JSON.stringify(arguments); | ||
| const serializerDefault = function() { | ||
| return JSON.stringify(arguments); | ||
| }; | ||
@@ -62,22 +59,20 @@ // | ||
| // | ||
| var ObjectWithoutPrototypeCache = /** @class */ (function () { | ||
| function ObjectWithoutPrototypeCache() { | ||
| this.cache = Object.create(null); | ||
| } | ||
| ObjectWithoutPrototypeCache.prototype.get = function (key) { | ||
| return this.cache[key]; | ||
| }; | ||
| ObjectWithoutPrototypeCache.prototype.set = function (key, value) { | ||
| this.cache[key] = value; | ||
| }; | ||
| return ObjectWithoutPrototypeCache; | ||
| }()); | ||
| var cacheDefault = { | ||
| create: function create() { | ||
| return new ObjectWithoutPrototypeCache(); | ||
| }, | ||
| class ObjectWithoutPrototypeCache { | ||
| cache; | ||
| constructor() { | ||
| this.cache = Object.create(null); | ||
| } | ||
| get(key) { | ||
| return this.cache[key]; | ||
| } | ||
| set(key, value) { | ||
| this.cache[key] = value; | ||
| } | ||
| } | ||
| const cacheDefault = { create: function create() { | ||
| return new ObjectWithoutPrototypeCache(); | ||
| } }; | ||
| export const strategies = { | ||
| variadic: strategyVariadic, | ||
| monadic: strategyMonadic | ||
| }; | ||
| export var strategies = { | ||
| variadic: strategyVariadic, | ||
| monadic: strategyMonadic, | ||
| }; |
+1
-1
| { | ||
| "name": "@formatjs/fast-memoize", | ||
| "description": "fork of fast-memoize and support esm", | ||
| "version": "3.0.2", | ||
| "version": "3.0.3", | ||
| "license": "MIT", | ||
@@ -6,0 +6,0 @@ "author": "Long Ho <holevietlong@gmail.com>", |
122
8.93%4930
-8.07%