import type { λ } from "./types"; /** * Returns a copy of `fun` that remembers its result for any given arguments and * only invokes `fun` for unknown arguments. * * The cache key is computed using the `key` function. The default `key` * function simply stringifies the arguments. * * If `limit` is specified, only the `limit`-last entries are kept in cache. * * The function's cache is available at `memoized.cache`. * * If `opt.weak` is `true`, non-primitive cache keys are stored in a WeakMap. * This behavior might for example be useful if you want to memoize some * calculation including a DOM Node without holding on to a reference of that * node. * Using weak keys prohibits setting a `limit`. * * @param fun - The function to be memoized. * @param opt - Optional additional parameters. * @returns The memoized function. * * @example * ``` * const expensiveCalculation = (a: number, b: number) => { * console.log(`calculate ${a} + ${b}`) * return a + b * } * const calc = memoize(expensiveCalculation) * * console.log( calc(1, 2) ) * // calculate 1 + 2 * // 3 * console.log( calc(20, 5) ) * // calculate 20 + 5 * // 25 * console.log( calc(20, 5) ) * // 25 * console.log( calc(1, 2) ) * // 3 * * calc.cache.clear() * console.log( calc(1, 2) ) * // calculate 1 + 2 * // 3 * ``` * * @example * ``` * const logIfDifferent = memoize( * (msg: string) => console.log(msg), * { * limit: 1, * key: msg => msg * } * ) * * logIfDifferent('a') * logIfDifferent('a') * logIfDifferent('b') * logIfDifferent('a') * * // a * // b * // a * ``` */ declare const memoize: , K = string, W extends boolean = false>(fun: T, opt?: { /** * How the cache key is computed. Defaults to `JSON.stringify`ing the arguments. */ key?: ((...args: Parameters) => K) | undefined; /** * The maximum number of results that can be kept in cache before discarding the oldest result. */ limit?: number | undefined; /** * Store non-primitive cache keys in a WeakMap. */ weak?: W | undefined; }) => T & { cache: W extends false ? Map> : Cache>; }; export default memoize; declare class Cache { #private; delete(key: K): boolean; has(key: K): boolean; get(key: K): V | undefined; set(key: K, value: V): void; }