source: node_modules/es-toolkit/dist/function/memoize.d.mts

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[a762898]1/**
2 * Creates a memoized version of the provided function. The memoized function caches
3 * results based on the argument it receives, so if the same argument is passed again,
4 * it returns the cached result instead of recomputing it.
5 *
6 * This function works with functions that take zero or just one argument. If your function
7 * originally takes multiple arguments, you should refactor it to take a single object or array
8 * that combines those arguments.
9 *
10 * If the argument is not primitive (e.g., arrays or objects), provide a
11 * `getCacheKey` function to generate a unique cache key for proper caching.
12 *
13 * @template F - The type of the function to be memoized.
14 * @param {F} fn - The function to be memoized. It should accept a single argument and return a value.
15 * @param {MemoizeOptions<Parameters<F>[0], ReturnType<F>>} [options={}] - Optional configuration for the memoization.
16 * @param {MemoizeCache<any, V>} [options.cache] - The cache object used to store results. Defaults to a new `Map`.
17 * @param {(args: A) => unknown} [options.getCacheKey] - An optional function to generate a unique cache key for each argument.
18 *
19 * @returns The memoized function with an additional `cache` property that exposes the internal cache.
20 *
21 * @example
22 * // Example using the default cache
23 * const add = (x: number) => x + 10;
24 * const memoizedAdd = memoize(add);
25 *
26 * console.log(memoizedAdd(5)); // 15
27 * console.log(memoizedAdd(5)); // 15 (cached result)
28 * console.log(memoizedAdd.cache.size); // 1
29 *
30 * @example
31 * // Example using a custom resolver
32 * const sum = (arr: number[]) => arr.reduce((x, y) => x + y, 0);
33 * const memoizedSum = memoize(sum, { getCacheKey: (arr: number[]) => arr.join(',') });
34 * console.log(memoizedSum([1, 2])); // 3
35 * console.log(memoizedSum([1, 2])); // 3 (cached result)
36 * console.log(memoizedSum.cache.size); // 1
37 *
38 * @example
39 * // Example using a custom cache implementation
40 * class CustomCache<K, T> implements MemoizeCache<K, T> {
41 * private cache = new Map<K, T>();
42 *
43 * set(key: K, value: T): void {
44 * this.cache.set(key, value);
45 * }
46 *
47 * get(key: K): T | undefined {
48 * return this.cache.get(key);
49 * }
50 *
51 * has(key: K): boolean {
52 * return this.cache.has(key);
53 * }
54 *
55 * delete(key: K): boolean {
56 * return this.cache.delete(key);
57 * }
58 *
59 * clear(): void {
60 * this.cache.clear();
61 * }
62 *
63 * get size(): number {
64 * return this.cache.size;
65 * }
66 * }
67 * const customCache = new CustomCache<string, number>();
68 * const memoizedSumWithCustomCache = memoize(sum, { cache: customCache });
69 * console.log(memoizedSumWithCustomCache([1, 2])); // 3
70 * console.log(memoizedSumWithCustomCache([1, 2])); // 3 (cached result)
71 * console.log(memoizedAddWithCustomCache.cache.size); // 1
72 */
73declare function memoize<F extends (...args: any) => any>(fn: F, options?: {
74 cache?: MemoizeCache<any, ReturnType<F>>;
75 getCacheKey?: (args: Parameters<F>[0]) => unknown;
76}): F & {
77 cache: MemoizeCache<any, ReturnType<F>>;
78};
79/**
80 * Represents a cache for memoization, allowing storage and retrieval of computed values.
81 *
82 * @template K - The type of keys used to store values in the cache.
83 * @template V - The type of values stored in the cache.
84 */
85interface MemoizeCache<K, V> {
86 /**
87 * Stores a value in the cache with the specified key.
88 *
89 * @param key - The key to associate with the value.
90 * @param value - The value to store in the cache.
91 */
92 set(key: K, value: V): void;
93 /**
94 * Retrieves a value from the cache by its key.
95 *
96 * @param key - The key of the value to retrieve.
97 * @returns The value associated with the key, or undefined if the key does not exist.
98 */
99 get(key: K): V | undefined;
100 /**
101 * Checks if a value exists in the cache for the specified key.
102 *
103 * @param key - The key to check for existence in the cache.
104 * @returns True if the cache contains the key, false otherwise.
105 */
106 has(key: K): boolean;
107 /**
108 * Deletes a value from the cache by its key.
109 *
110 * @param key - The key of the value to delete.
111 * @returns True if the value was successfully deleted, false otherwise.
112 */
113 delete(key: K): boolean | void;
114 /**
115 * Clears all values from the cache.
116 */
117 clear(): void;
118 /**
119 * The number of entries in the cache.
120 */
121 size: number;
122}
123
124export { type MemoizeCache, memoize };
Note: See TracBrowser for help on using the repository browser.