UNPKG

cache-func

Version:

A tiny utility to cache function outputs easily

21 lines (20 loc) 408 B
// src/index.ts function cacheFunc(fn, options) { let val; let lastRun; function wrapper(...args) { if (val === void 0 || options?.maxAge && lastRun !== void 0 && Date.now() - lastRun > options.maxAge) { val = fn(...args); lastRun = Date.now(); } return val; } wrapper.clear = () => { val = void 0; lastRun = void 0; }; return wrapper; } export { cacheFunc };