//@ts-check /** * Caches the result of a function. * The cache is available as `.cache` in case there's a need to clear anything. * @template {(...args: any[]) => any} T * @param {T} functionToMemoize * @returns */ export const memoize = (functionToMemoize) => { /** @type {Map[0], ReturnType>} */ const cache = new Map() /** * * @param {Parameters} args * @returns {ReturnType} */ const memoized = (...args) => { const key = args[0] if(!cache.has(key)){ cache.set(key, functionToMemoize(...args)) } //@ts-ignore return cache.get(key) } memoized.map = cache return memoized } export default memoize