index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * lodash 3.0.4 (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modern modularize exports="npm" -o ./`
  4. * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <https://lodash.com/license>
  8. */
  9. /** Used as the `TypeError` message for "Functions" methods. */
  10. var FUNC_ERROR_TEXT = 'Expected a function';
  11. /** Used for native method references. */
  12. var objectProto = Object.prototype;
  13. /** Used to check objects for own properties. */
  14. var hasOwnProperty = objectProto.hasOwnProperty;
  15. /**
  16. * Creates a cache object to store key/value pairs.
  17. *
  18. * @private
  19. * @static
  20. * @name Cache
  21. * @memberOf _.memoize
  22. */
  23. function MapCache() {
  24. this.__data__ = {};
  25. }
  26. /**
  27. * Removes `key` and its value from the cache.
  28. *
  29. * @private
  30. * @name delete
  31. * @memberOf _.memoize.Cache
  32. * @param {string} key The key of the value to remove.
  33. * @returns {boolean} Returns `true` if the entry was removed successfully, else `false`.
  34. */
  35. function mapDelete(key) {
  36. return this.has(key) && delete this.__data__[key];
  37. }
  38. /**
  39. * Gets the cached value for `key`.
  40. *
  41. * @private
  42. * @name get
  43. * @memberOf _.memoize.Cache
  44. * @param {string} key The key of the value to get.
  45. * @returns {*} Returns the cached value.
  46. */
  47. function mapGet(key) {
  48. return key == '__proto__' ? undefined : this.__data__[key];
  49. }
  50. /**
  51. * Checks if a cached value for `key` exists.
  52. *
  53. * @private
  54. * @name has
  55. * @memberOf _.memoize.Cache
  56. * @param {string} key The key of the entry to check.
  57. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  58. */
  59. function mapHas(key) {
  60. return key != '__proto__' && hasOwnProperty.call(this.__data__, key);
  61. }
  62. /**
  63. * Sets `value` to `key` of the cache.
  64. *
  65. * @private
  66. * @name set
  67. * @memberOf _.memoize.Cache
  68. * @param {string} key The key of the value to cache.
  69. * @param {*} value The value to cache.
  70. * @returns {Object} Returns the cache object.
  71. */
  72. function mapSet(key, value) {
  73. if (key != '__proto__') {
  74. this.__data__[key] = value;
  75. }
  76. return this;
  77. }
  78. /**
  79. * Creates a function that memoizes the result of `func`. If `resolver` is
  80. * provided it determines the cache key for storing the result based on the
  81. * arguments provided to the memoized function. By default, the first argument
  82. * provided to the memoized function is coerced to a string and used as the
  83. * cache key. The `func` is invoked with the `this` binding of the memoized
  84. * function.
  85. *
  86. * **Note:** The cache is exposed as the `cache` property on the memoized
  87. * function. Its creation may be customized by replacing the `_.memoize.Cache`
  88. * constructor with one whose instances implement the [`Map`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-the-map-prototype-object)
  89. * method interface of `get`, `has`, and `set`.
  90. *
  91. * @static
  92. * @memberOf _
  93. * @category Function
  94. * @param {Function} func The function to have its output memoized.
  95. * @param {Function} [resolver] The function to resolve the cache key.
  96. * @returns {Function} Returns the new memoizing function.
  97. * @example
  98. *
  99. * var upperCase = _.memoize(function(string) {
  100. * return string.toUpperCase();
  101. * });
  102. *
  103. * upperCase('fred');
  104. * // => 'FRED'
  105. *
  106. * // modifying the result cache
  107. * upperCase.cache.set('fred', 'BARNEY');
  108. * upperCase('fred');
  109. * // => 'BARNEY'
  110. *
  111. * // replacing `_.memoize.Cache`
  112. * var object = { 'user': 'fred' };
  113. * var other = { 'user': 'barney' };
  114. * var identity = _.memoize(_.identity);
  115. *
  116. * identity(object);
  117. * // => { 'user': 'fred' }
  118. * identity(other);
  119. * // => { 'user': 'fred' }
  120. *
  121. * _.memoize.Cache = WeakMap;
  122. * var identity = _.memoize(_.identity);
  123. *
  124. * identity(object);
  125. * // => { 'user': 'fred' }
  126. * identity(other);
  127. * // => { 'user': 'barney' }
  128. */
  129. function memoize(func, resolver) {
  130. if (typeof func != 'function' || (resolver && typeof resolver != 'function')) {
  131. throw new TypeError(FUNC_ERROR_TEXT);
  132. }
  133. var memoized = function() {
  134. var args = arguments,
  135. key = resolver ? resolver.apply(this, args) : args[0],
  136. cache = memoized.cache;
  137. if (cache.has(key)) {
  138. return cache.get(key);
  139. }
  140. var result = func.apply(this, args);
  141. memoized.cache = cache.set(key, result);
  142. return result;
  143. };
  144. memoized.cache = new memoize.Cache;
  145. return memoized;
  146. }
  147. // Add functions to the `Map` cache.
  148. MapCache.prototype['delete'] = mapDelete;
  149. MapCache.prototype.get = mapGet;
  150. MapCache.prototype.has = mapHas;
  151. MapCache.prototype.set = mapSet;
  152. // Assign cache to `_.memoize`.
  153. memoize.Cache = MapCache;
  154. module.exports = memoize;