// https://github.com/angus-c/just/blob/master/packages/function-memoize/index.mjs type func = (...args: any) => any; export function memoize( callback: T, { resolver = (...args: Parameters) => JSON.stringify(args), }: { resolver?: (...args: Parameters) => string } = {}, ): T { if (typeof callback !== 'function') { throw new Error('`callback` should be a function'); } if (resolver !== undefined && typeof resolver !== 'function') { throw new Error('`resolver` should be a function'); } const cache: Record = {}; const memoized = function (this: any, ...args: Parameters) { const key = resolver.apply(this, args); if (!(key in cache)) { cache[key] = callback.apply(this, args); } return cache[key]; }; memoized.cache = cache; return memoized as func as T; }