memoizeCapped.d.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Creates a function that memoizes the result of `func`. If `resolver` is
  3. * provided, it determines the cache key for storing the result based on the
  4. * arguments provided to the memoized function. By default, the first argument
  5. * provided to the memoized function is used as the map cache key. The `func`
  6. * is invoked with the `this` binding of the memoized function.
  7. *
  8. * **Note:** The cache is exposed as the `cache` property on the memoized
  9. * function. Its creation may be customized by replacing the `memoize.Cache`
  10. * constructor with one whose instances implement the
  11. * [`Map`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object)
  12. * method interface of `clear`, `delete`, `get`, `has`, and `set`.
  13. *
  14. * @since 0.1.0
  15. * @category Function
  16. * @param {Function} func The function to have its output memoized.
  17. * @param {Function} [resolver] The function to resolve the cache key.
  18. * @returns {Function} Returns the new memoized function.
  19. * @example
  20. *
  21. * const object = { 'a': 1, 'b': 2 }
  22. * const other = { 'c': 3, 'd': 4 }
  23. *
  24. * const values = memoize(values)
  25. * values(object)
  26. * // => [1, 2]
  27. *
  28. * values(other)
  29. * // => [3, 4]
  30. *
  31. * object.a = 2
  32. * values(object)
  33. * // => [1, 2]
  34. *
  35. * // Modify the result cache.
  36. * values.cache.set(object, ['a', 'b'])
  37. * values(object)
  38. * // => ['a', 'b']
  39. *
  40. * // Replace `memoize.Cache`.
  41. * memoize.Cache = WeakMap
  42. */
  43. declare function memoize(func: any, resolver: any): {
  44. (...args: any[]): any;
  45. cache: Map<any, any>;
  46. };
  47. declare namespace memoize {
  48. var Cache: MapConstructor;
  49. }
  50. export default memoize;