UNPKG

@tangelo/tangelo-configuration-toolkit

Version:

Tangelo Configuration Toolkit is a command-line toolkit which offers support for developing a Tangelo configuration.

28 lines (25 loc) 778 B
/** * Memoizes a function by caching its results based on the arguments. * @param {Function} fn - The function to be memoized. * @returns {Function} - The memoized function. */ module.exports = function memoize(fn) { let cache = {}; /** * The memoized function that caches the results of the original function based on its arguments. * @param {...*} args - The arguments to be passed to the original function. * @returns {*} - The result of the original function. */ const cachedfn = (...args) => { const key = JSON.stringify(args); cache[key] ??= fn(...args); return cache[key]; }; /** * Clears the cache of the memoized function. */ cachedfn.clear = () => { cache = {}; }; return cachedfn; };