react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / lodash / function / memoize.js
80742 viewsvar MapCache = require('../internal/MapCache');12/** Used as the `TypeError` message for "Functions" methods. */3var FUNC_ERROR_TEXT = 'Expected a function';45/**6* Creates a function that memoizes the result of `func`. If `resolver` is7* provided it determines the cache key for storing the result based on the8* arguments provided to the memoized function. By default, the first argument9* provided to the memoized function is coerced to a string and used as the10* cache key. The `func` is invoked with the `this` binding of the memoized11* function.12*13* **Note:** The cache is exposed as the `cache` property on the memoized14* function. Its creation may be customized by replacing the `_.memoize.Cache`15* constructor with one whose instances implement the [`Map`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-the-map-prototype-object)16* method interface of `get`, `has`, and `set`.17*18* @static19* @memberOf _20* @category Function21* @param {Function} func The function to have its output memoized.22* @param {Function} [resolver] The function to resolve the cache key.23* @returns {Function} Returns the new memoizing function.24* @example25*26* var upperCase = _.memoize(function(string) {27* return string.toUpperCase();28* });29*30* upperCase('fred');31* // => 'FRED'32*33* // modifying the result cache34* upperCase.cache.set('fred', 'BARNEY');35* upperCase('fred');36* // => 'BARNEY'37*38* // replacing `_.memoize.Cache`39* var object = { 'user': 'fred' };40* var other = { 'user': 'barney' };41* var identity = _.memoize(_.identity);42*43* identity(object);44* // => { 'user': 'fred' }45* identity(other);46* // => { 'user': 'fred' }47*48* _.memoize.Cache = WeakMap;49* var identity = _.memoize(_.identity);50*51* identity(object);52* // => { 'user': 'fred' }53* identity(other);54* // => { 'user': 'barney' }55*/56function memoize(func, resolver) {57if (typeof func != 'function' || (resolver && typeof resolver != 'function')) {58throw new TypeError(FUNC_ERROR_TEXT);59}60var memoized = function() {61var args = arguments,62key = resolver ? resolver.apply(this, args) : args[0],63cache = memoized.cache;6465if (cache.has(key)) {66return cache.get(key);67}68var result = func.apply(this, args);69memoized.cache = cache.set(key, result);70return result;71};72memoized.cache = new memoize.Cache;73return memoized;74}7576// Assign cache to `_.memoize`.77memoize.Cache = MapCache;7879module.exports = memoize;808182