| 1 | interface MapCache {
|
|---|
| 2 | /**
|
|---|
| 3 | * Removes the value associated with the specified key from the cache.
|
|---|
| 4 | *
|
|---|
| 5 | * @param key - The key of the value to remove
|
|---|
| 6 | * @returns `true` if an element was removed, `false` if the key wasn't found
|
|---|
| 7 | *
|
|---|
| 8 | * @example
|
|---|
| 9 | * ```typescript
|
|---|
| 10 | * cache.set('user', { id: 123, name: 'John' });
|
|---|
| 11 | * cache.delete('user'); // Returns true
|
|---|
| 12 | * cache.delete('unknown'); // Returns false
|
|---|
| 13 | * ```
|
|---|
| 14 | */
|
|---|
| 15 | delete(key: any): boolean;
|
|---|
| 16 | /**
|
|---|
| 17 | * Retrieves the value associated with the specified key from the cache.
|
|---|
| 18 | *
|
|---|
| 19 | * @param key - The key of the value to retrieve
|
|---|
| 20 | * @returns The cached value or undefined if not found
|
|---|
| 21 | *
|
|---|
| 22 | * @example
|
|---|
| 23 | * ```typescript
|
|---|
| 24 | * cache.set('user', { id: 123, name: 'John' });
|
|---|
| 25 | * cache.get('user'); // Returns { id: 123, name: 'John' }
|
|---|
| 26 | * cache.get('unknown'); // Returns undefined
|
|---|
| 27 | * ```
|
|---|
| 28 | */
|
|---|
| 29 | get(key: any): any;
|
|---|
| 30 | /**
|
|---|
| 31 | * Checks if the cache contains a value for the specified key.
|
|---|
| 32 | *
|
|---|
| 33 | * @param key - The key to check for existence
|
|---|
| 34 | * @returns `true` if the key exists in the cache, otherwise `false`
|
|---|
| 35 | *
|
|---|
| 36 | * @example
|
|---|
| 37 | * ```typescript
|
|---|
| 38 | * cache.set('user', { id: 123, name: 'John' });
|
|---|
| 39 | * cache.has('user'); // Returns true
|
|---|
| 40 | * cache.has('unknown'); // Returns false
|
|---|
| 41 | * ```
|
|---|
| 42 | */
|
|---|
| 43 | has(key: any): boolean;
|
|---|
| 44 | /**
|
|---|
| 45 | * Stores a value in the cache with the specified key.
|
|---|
| 46 | * If the key already exists, its value is updated.
|
|---|
| 47 | *
|
|---|
| 48 | * @param key - The key to associate with the value
|
|---|
| 49 | * @param value - The value to store in the cache
|
|---|
| 50 | * @returns The cache instance for method chaining
|
|---|
| 51 | *
|
|---|
| 52 | * @example
|
|---|
| 53 | * ```typescript
|
|---|
| 54 | * cache.set('user', { id: 123, name: 'John' })
|
|---|
| 55 | * .set('settings', { theme: 'dark' });
|
|---|
| 56 | * ```
|
|---|
| 57 | */
|
|---|
| 58 | set(key: any, value: any): this;
|
|---|
| 59 | /**
|
|---|
| 60 | * Removes all key-value pairs from the cache.
|
|---|
| 61 | * This method is optional as some cache implementations may be immutable.
|
|---|
| 62 | *
|
|---|
| 63 | * @example
|
|---|
| 64 | * ```typescript
|
|---|
| 65 | * cache.set('user', { id: 123, name: 'John' });
|
|---|
| 66 | * cache.set('settings', { theme: 'dark' });
|
|---|
| 67 | * cache.clear(); // Cache is now empty
|
|---|
| 68 | * ```
|
|---|
| 69 | */
|
|---|
| 70 | clear?(): void;
|
|---|
| 71 | }
|
|---|
| 72 | /**
|
|---|
| 73 | * Constructor interface for creating a new MapCache instance.
|
|---|
| 74 | * This defines the shape of a constructor that can create cache objects
|
|---|
| 75 | * conforming to the MapCache interface.
|
|---|
| 76 | *
|
|---|
| 77 | * @example
|
|---|
| 78 | * ```typescript
|
|---|
| 79 | * class CustomCache implements MapCache {
|
|---|
| 80 | * // Cache implementation
|
|---|
| 81 | * }
|
|---|
| 82 | *
|
|---|
| 83 | * const CacheConstructor: MapCacheConstructor = CustomCache;
|
|---|
| 84 | * const cache = new CacheConstructor();
|
|---|
| 85 | * ```
|
|---|
| 86 | */
|
|---|
| 87 | interface MapCacheConstructor {
|
|---|
| 88 | new (): MapCache;
|
|---|
| 89 | }
|
|---|
| 90 | /**
|
|---|
| 91 | * Represents a function that has been memoized.
|
|---|
| 92 | * A memoized function maintains the same signature as the original function
|
|---|
| 93 | * but adds a cache property to store previously computed results.
|
|---|
| 94 | *
|
|---|
| 95 | * @template T - The type of the original function being memoized
|
|---|
| 96 | */
|
|---|
| 97 | interface MemoizedFunction {
|
|---|
| 98 | /**
|
|---|
| 99 | * The cache storing previously computed results
|
|---|
| 100 | */
|
|---|
| 101 | cache: MapCache;
|
|---|
| 102 | }
|
|---|
| 103 | /**
|
|---|
| 104 | * Creates a function that memoizes the result of func. If resolver is provided it determines the cache key for
|
|---|
| 105 | * storing the result based on the arguments provided to the memoized function. By default, the first argument
|
|---|
| 106 | * provided to the memoized function is coerced to a string and used as the cache key. The func is invoked with
|
|---|
| 107 | * the this binding of the memoized function.
|
|---|
| 108 | *
|
|---|
| 109 | * @template T - The type of the original function being memoized
|
|---|
| 110 | * @param {T} func The function to have its output memoized.
|
|---|
| 111 | * @param {Function} [resolver] The function to resolve the cache key.
|
|---|
| 112 | * @return {MemoizedFunction<T>} Returns the new memoizing function.
|
|---|
| 113 | */
|
|---|
| 114 | declare function memoize<T extends (...args: any) => any>(func: T, resolver?: (...args: Parameters<T>) => any): T & MemoizedFunction;
|
|---|
| 115 | declare namespace memoize {
|
|---|
| 116 | var Cache: MapCacheConstructor;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | export { memoize };
|
|---|