index.d.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. declare type AnyFunction = (...arguments_: any) => any;
  2. interface CacheStorageContent<ValueType> {
  3. data: ValueType;
  4. maxAge: number;
  5. }
  6. interface CacheStorage<KeyType, ValueType> {
  7. has: (key: KeyType) => boolean;
  8. get: (key: KeyType) => CacheStorageContent<ValueType> | undefined;
  9. set: (key: KeyType, value: CacheStorageContent<ValueType>) => void;
  10. delete: (key: KeyType) => void;
  11. clear?: () => void;
  12. }
  13. interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {
  14. /**
  15. Milliseconds until the cache expires.
  16. @default Infinity
  17. */
  18. readonly maxAge?: number;
  19. /**
  20. Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
  21. A `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option).
  22. You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible:
  23. ```
  24. import mem = require('mem');
  25. mem(function_, {cacheKey: JSON.stringify});
  26. ```
  27. Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for `RegExp`, `Date` and so on.
  28. ```
  29. import mem = require('mem');
  30. import serializeJavascript = require('serialize-javascript');
  31. mem(function_, {cacheKey: serializeJavascript});
  32. ```
  33. @default arguments_ => arguments_[0]
  34. @example arguments_ => JSON.stringify(arguments_)
  35. */
  36. readonly cacheKey?: (arguments_: Parameters<FunctionToMemoize>) => CacheKeyType;
  37. /**
  38. 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.
  39. @default new Map()
  40. @example new WeakMap()
  41. */
  42. readonly cache?: CacheStorage<CacheKeyType, ReturnType<FunctionToMemoize>>;
  43. }
  44. /**
  45. [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.
  46. @param fn - Function to be memoized.
  47. @example
  48. ```
  49. import mem = require('mem');
  50. let i = 0;
  51. const counter = () => ++i;
  52. const memoized = mem(counter);
  53. memoized('foo');
  54. //=> 1
  55. // Cached as it's the same arguments
  56. memoized('foo');
  57. //=> 1
  58. // Not cached anymore as the arguments changed
  59. memoized('bar');
  60. //=> 2
  61. memoized('bar');
  62. //=> 2
  63. ```
  64. */
  65. declare const mem: {
  66. <FunctionToMemoize extends AnyFunction, CacheKeyType>(fn: FunctionToMemoize, { cacheKey, cache, maxAge }?: Options<FunctionToMemoize, CacheKeyType>): FunctionToMemoize;
  67. /**
  68. @returns A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
  69. @example
  70. ```
  71. import mem = require('mem');
  72. class Example {
  73. index = 0
  74. @mem.decorator()
  75. counter() {
  76. return ++this.index;
  77. }
  78. }
  79. class ExampleWithOptions {
  80. index = 0
  81. @mem.decorator({maxAge: 1000})
  82. counter() {
  83. return ++this.index;
  84. }
  85. }
  86. ```
  87. */
  88. decorator<FunctionToMemoize_1 extends AnyFunction, CacheKeyType_1>(options?: Options<FunctionToMemoize_1, CacheKeyType_1>): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
  89. /**
  90. Clear all cached data of a memoized function.
  91. @param fn - Memoized function.
  92. */
  93. clear(fn: AnyFunction): void;
  94. };
  95. export = mem;