|
1 |
| -export interface CacheStorage< |
2 |
| - KeyType extends unknown, |
3 |
| - ValueType extends unknown |
4 |
| -> { |
5 |
| - has(key: KeyType): boolean; |
6 |
| - get(key: KeyType): ValueType | undefined; |
7 |
| - set(key: KeyType, value: ValueType): void; |
8 |
| - delete(key: KeyType): void; |
9 |
| - clear?: () => void; |
10 |
| -} |
| 1 | +declare namespace mem { |
| 2 | + interface CacheStorage<KeyType extends unknown, ValueType extends unknown> { |
| 3 | + has(key: KeyType): boolean; |
| 4 | + get(key: KeyType): ValueType | undefined; |
| 5 | + set(key: KeyType, value: ValueType): void; |
| 6 | + delete(key: KeyType): void; |
| 7 | + clear?: () => void; |
| 8 | + } |
11 | 9 |
|
12 |
| -export interface Options< |
13 |
| - ArgumentsType extends unknown[], |
14 |
| - CacheKeyType extends unknown, |
15 |
| - ReturnType extends unknown |
16 |
| -> { |
17 |
| - /** |
18 |
| - * Milliseconds until the cache expires. |
19 |
| - * |
20 |
| - * @default Infinity |
21 |
| - */ |
22 |
| - readonly maxAge?: number; |
| 10 | + interface Options< |
| 11 | + ArgumentsType extends unknown[], |
| 12 | + CacheKeyType extends unknown, |
| 13 | + ReturnType extends unknown |
| 14 | + > { |
| 15 | + /** |
| 16 | + Milliseconds until the cache expires. |
23 | 17 |
|
24 |
| - /** |
25 |
| - * Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array. |
26 |
| - * |
27 |
| - * You could for example change it to only cache on the first argument `x => JSON.stringify(x)`. |
28 |
| - */ |
29 |
| - readonly cacheKey?: (...arguments: ArgumentsType) => CacheKeyType; |
| 18 | + @default Infinity |
| 19 | + */ |
| 20 | + readonly maxAge?: number; |
30 | 21 |
|
31 |
| - /** |
32 |
| - * Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, `.delete(key)`, and optionally `.clear()`. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache. |
33 |
| - * |
34 |
| - * @default new Map() |
35 |
| - */ |
36 |
| - readonly cache?: CacheStorage<CacheKeyType, {data: ReturnType; maxAge: number}>; |
| 22 | + /** |
| 23 | + Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array. |
37 | 24 |
|
38 |
| - /** |
39 |
| - * Cache rejected promises. |
40 |
| - * |
41 |
| - * @default false |
42 |
| - */ |
43 |
| - readonly cachePromiseRejection?: boolean; |
| 25 | + You could for example change it to only cache on the first argument `x => JSON.stringify(x)`. |
| 26 | + */ |
| 27 | + readonly cacheKey?: (...arguments: ArgumentsType) => CacheKeyType; |
| 28 | + |
| 29 | + /** |
| 30 | + Use a different cache storage. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache. |
| 31 | +
|
| 32 | + @default new Map() |
| 33 | + */ |
| 34 | + readonly cache?: CacheStorage<CacheKeyType, {data: ReturnType; maxAge: number}>; |
| 35 | + |
| 36 | + /** |
| 37 | + Cache rejected promises. |
| 38 | +
|
| 39 | + @default false |
| 40 | + */ |
| 41 | + readonly cachePromiseRejection?: boolean; |
| 42 | + } |
44 | 43 | }
|
45 | 44 |
|
46 |
| -/** |
47 |
| - * [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input. |
48 |
| - * |
49 |
| - * @param fn - Function to be memoized. |
50 |
| - */ |
51 | 45 | declare const mem: {
|
| 46 | + /** |
| 47 | + [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input. |
| 48 | +
|
| 49 | + @param fn - Function to be memoized. |
| 50 | +
|
| 51 | + @example |
| 52 | + ``` |
| 53 | + import mem = require('mem'); |
| 54 | +
|
| 55 | + let i = 0; |
| 56 | + const counter = () => ++i; |
| 57 | + const memoized = mem(counter); |
| 58 | +
|
| 59 | + memoized('foo'); |
| 60 | + //=> 1 |
| 61 | +
|
| 62 | + // Cached as it's the same arguments |
| 63 | + memoized('foo'); |
| 64 | + //=> 1 |
| 65 | +
|
| 66 | + // Not cached anymore as the arguments changed |
| 67 | + memoized('bar'); |
| 68 | + //=> 2 |
| 69 | +
|
| 70 | + memoized('bar'); |
| 71 | + //=> 2 |
| 72 | + ``` |
| 73 | + */ |
52 | 74 | <
|
53 | 75 | ArgumentsType extends unknown[],
|
54 | 76 | ReturnType extends unknown,
|
55 | 77 | CacheKeyType extends unknown
|
56 | 78 | >(
|
57 | 79 | fn: (...arguments: ArgumentsType) => ReturnType,
|
58 |
| - options?: Options<ArgumentsType, CacheKeyType, ReturnType> |
| 80 | + options?: mem.Options<ArgumentsType, CacheKeyType, ReturnType> |
59 | 81 | ): (...arguments: ArgumentsType) => ReturnType;
|
60 | 82 |
|
61 | 83 | /**
|
62 |
| - * Clear all cached data of a memoized function. |
63 |
| - * |
64 |
| - * @param fn - Memoized function. |
65 |
| - */ |
| 84 | + Clear all cached data of a memoized function. |
| 85 | +
|
| 86 | + @param fn - Memoized function. |
| 87 | + */ |
66 | 88 | clear<ArgumentsType extends unknown[], ReturnType extends unknown>(
|
67 | 89 | fn: (...arguments: ArgumentsType) => ReturnType
|
68 | 90 | ): void;
|
| 91 | + |
| 92 | + // TODO: Remove this for the next major release |
| 93 | + default: typeof mem; |
69 | 94 | };
|
70 | 95 |
|
71 |
| -export default mem; |
| 96 | +export = mem; |
0 commit comments