(fn: (arg: T) => R)
| 213 | * Memoizes the single-parameter function. |
| 214 | */ |
| 215 | export function memoize<T, R>(fn: (arg: T) => R): ((arg: T) => R) & { clear(): void } { |
| 216 | const cached = new Map<T, R>(); |
| 217 | const wrapper = (arg: T): R => { |
| 218 | if (cached.has(arg)) { |
| 219 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 220 | return cached.get(arg)!; |
| 221 | } |
| 222 | |
| 223 | const value = fn(arg); |
| 224 | cached.set(arg, value); |
| 225 | return value; |
| 226 | }; |
| 227 | |
| 228 | wrapper.clear = () => cached.clear(); |
| 229 | |
| 230 | return wrapper; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Debounces the function call for an interval. |
no test coverage detected