UNPKG

diffusion

Version:

Diffusion JavaScript client

41 lines (40 loc) 1.13 kB
"use strict"; /** * @module Util.Function */ Object.defineProperty(exports, "__esModule", { value: true }); exports.memoize = void 0; var function_1 = require("./function"); /** * Wrap a function with an automatic memoization layer. * * @param function the function to cache results for. * @param cache the internal cache to use. * @param matcher the matcher function to return a cache key from an array of arguments * * @returns the memoized function */ function memoize(f, cache, matcher) { if (cache === void 0) { cache = {}; } if (matcher === void 0) { matcher = function_1.identity; } var c = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var a = matcher(args); var r = cache[a]; if (r) { return r; } else { r = f.apply(f, args); } cache[a] = r; return r; }; // this is only used for unit testing to gain access to the uncached function c._uncached = f; return c; } exports.memoize = memoize;