function.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.maxArgumentCount = maxArgumentCount;
  7. exports.memoize = memoize;
  8. exports.memoizeCompare = memoizeCompare;
  9. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  10. var _lruQueue = require("./lruQueue.js");
  11. // function utils
  12. /**
  13. * Memoize a given function by caching the computed result.
  14. * The cache of a memoized function can be cleared by deleting the `cache`
  15. * property of the function.
  16. *
  17. * @param {function} fn The function to be memoized.
  18. * Must be a pure function.
  19. * @param {Object} [options]
  20. * @param {function(args: Array): string} [options.hasher]
  21. * A custom hash builder. Is JSON.stringify by default.
  22. * @param {number | undefined} [options.limit]
  23. * Maximum number of values that may be cached. Undefined indicates
  24. * unlimited (default)
  25. * @return {function} Returns the memoized function
  26. */
  27. function memoize(fn) {
  28. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  29. hasher = _ref.hasher,
  30. limit = _ref.limit;
  31. limit = limit == null ? Number.POSITIVE_INFINITY : limit;
  32. hasher = hasher == null ? JSON.stringify : hasher;
  33. return function memoize() {
  34. if ((0, _typeof2["default"])(memoize.cache) !== 'object') {
  35. memoize.cache = {
  36. values: new Map(),
  37. lru: (0, _lruQueue.lruQueue)(limit || Number.POSITIVE_INFINITY)
  38. };
  39. }
  40. var args = [];
  41. for (var i = 0; i < arguments.length; i++) {
  42. args[i] = arguments[i];
  43. }
  44. var hash = hasher(args);
  45. if (memoize.cache.values.has(hash)) {
  46. memoize.cache.lru.hit(hash);
  47. return memoize.cache.values.get(hash);
  48. }
  49. var newVal = fn.apply(fn, args);
  50. memoize.cache.values.set(hash, newVal);
  51. memoize.cache.values["delete"](memoize.cache.lru.hit(hash));
  52. return newVal;
  53. };
  54. }
  55. /**
  56. * Memoize a given function by caching all results and the arguments,
  57. * and comparing against the arguments of previous results before
  58. * executing again.
  59. * This is less performant than `memoize` which calculates a hash,
  60. * which is very fast to compare. Use `memoizeCompare` only when it is
  61. * not possible to create a unique serializable hash from the function
  62. * arguments.
  63. * The isEqual function must compare two sets of arguments
  64. * and return true when equal (can be a deep equality check for example).
  65. * @param {function} fn
  66. * @param {function(a: *, b: *) : boolean} isEqual
  67. * @returns {function}
  68. */
  69. function memoizeCompare(fn, isEqual) {
  70. var memoize = function memoize() {
  71. var args = [];
  72. for (var i = 0; i < arguments.length; i++) {
  73. args[i] = arguments[i];
  74. }
  75. for (var c = 0; c < memoize.cache.length; c++) {
  76. var cached = memoize.cache[c];
  77. if (isEqual(args, cached.args)) {
  78. // TODO: move this cache entry to the top so recently used entries move up?
  79. return cached.res;
  80. }
  81. }
  82. var res = fn.apply(fn, args);
  83. memoize.cache.unshift({
  84. args: args,
  85. res: res
  86. });
  87. return res;
  88. };
  89. memoize.cache = [];
  90. return memoize;
  91. }
  92. /**
  93. * Find the maximum number of arguments expected by a typed function.
  94. * @param {function} fn A typed function
  95. * @return {number} Returns the maximum number of expected arguments.
  96. * Returns -1 when no signatures where found on the function.
  97. */
  98. function maxArgumentCount(fn) {
  99. return Object.keys(fn.signatures || {}).reduce(function (args, signature) {
  100. var count = (signature.match(/,/g) || []).length + 1;
  101. return Math.max(args, count);
  102. }, -1);
  103. }