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