deleight
Version:
A library with 9 modules for writing more expressive web applications with traditional HTML, CSS and JavaScript.
59 lines (58 loc) • 1.54 kB
JavaScript
;
/**
* Contains functions for caching function results. Pending tests.
* Please report bugs.
*
* @module
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.cacheWith = exports.cache = void 0;
/**
* Caches the return value of calling a function. Repeat calls
* will simply return the cached value.
*
* Pending tests. Please report bugs.
*
* @example
*
*/
function cache(func) {
let result, called = false;
return Object.assign(function (...args) {
if (called)
return result;
result = func.call(this, ...args);
called = true;
}, { reset: () => { called = false; } });
}
exports.cache = cache;
const hashes = new WeakMap();
function identityHash(object) {
if (hashes.has(object))
return hashes.get(object);
else {
const randomHash = `${Math.random()}`;
hashes.set(object, randomHash);
return randomHash;
}
}
/**
* Caches the return value of calling a function. using the arguments as keys.
* This is also called memoization.
*
* Pending tests. Please report bugs.
*
* @example
*
*/
function cacheWith(func, hash = identityHash) {
let results = {};
return Object.assign(function (...args) {
const argsHash = args.map(arg => hash(arg)).join('-|-');
if (!results.hasOwnProperty(argsHash)) {
results[argsHash] = func.call(this, ...args);
}
return results[argsHash];
}, { reset: () => { results = {}; } });
}
exports.cacheWith = cacheWith;