Make _.memoize avoid using keyPrefix if passed a resolver function. [closes #330]

Former-commit-id: 97e3bb353d988c92eea394dfb496ebf7594ae25f
This commit is contained in:
John-David Dalton
2013-08-19 22:42:15 -07:00
parent 1901134601
commit a562126f2f
8 changed files with 103 additions and 33 deletions

16
dist/lodash.compat.js vendored
View File

@@ -5472,6 +5472,20 @@
* var fibonacci = _.memoize(function(n) {
* return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
* });
*
* var data = {
* 'moe': { 'name': 'moe', 'age': 40 },
* 'curly': { 'name': 'curly', 'age': 60 }
* };
*
* // modifying the result cache
* var stooge = _.memoize(function(name) { return data[name]; }, _.identity);
* stooge('curly');
* // => { 'name': 'curly', 'age': 60 }
*
* stooge.cache.curly.name = 'jerome';
* stooge('curly');
* // => { 'name': 'jerome', 'age': 60 }
*/
function memoize(func, resolver) {
if (!isFunction(func)) {
@@ -5479,7 +5493,7 @@
}
var memoized = function() {
var cache = memoized.cache,
key = keyPrefix + (resolver ? resolver.apply(this, arguments) : arguments[0]);
key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0];
return hasOwnProperty.call(cache, key)
? cache[key]

View File

@@ -35,7 +35,7 @@ mt(/x/)&&(mt=function(n){return typeof n=="function"&&ke.call(n)==J});var er=ye?
},_.chain=function(n){return n=new w(n),n.__chain__=m,n},_.compact=function(n){for(var t=-1,e=n?n.length:0,r=[];++t<e;){var u=n[t];u&&r.push(u)}return r},_.compose=function(){for(var n=arguments,t=n.length||1;t--;)if(!mt(n[t]))throw new oe;return function(){for(var t=arguments,e=n.length;e--;)t=[n[e].apply(this,t)];return t[0]}},_.countBy=rr,_.createCallback=function(n,t,e){var r=typeof n;if(n==d||"function"==r)return Y(n,t,e);if("object"!=r)return function(t){return t[n]};var u=We(n),o=u[0],a=n[o];
return 1!=u.length||a!==a||dt(a)?function(t){for(var e=u.length,r=b;e--&&(r=tt(t[u[e]],n[u[e]],d,m)););return r}:function(n){return n=n[o],a===n&&(0!==a||1/a==1/n)}},_.curry=function(n,t){return t=typeof t=="number"?t:+t||n.length,at(n,4,d,d,d,t)},_.debounce=Gt,_.defaults=Ze,_.defer=Jt,_.delay=function(n,t){if(!mt(n))throw new oe;var e=Fe.call(arguments,2);return we(function(){n.apply(y,e)},t)},_.difference=Ft,_.filter=xt,_.flatten=function(n,t,e,r){return typeof t!="boolean"&&t!=d&&(r=e,e=r&&r[t]===n?y:t,t=b),e!=d&&(n=St(n,e,r)),Z(n,t)
},_.forEach=Et,_.forEachRight=Ot,_.forIn=nr,_.forInRight=function(n,t,e){var r=[];nr(n,function(n,t){r.push(t,n)});var u=r.length;for(t=Y(t,e,3);u--&&t(r[u--],r[u],n)!==false;);return n},_.forOwn=tr,_.forOwnRight=ht,_.functions=vt,_.groupBy=ur,_.indexBy=or,_.initial=function(n,t,e){if(!n)return[];var r=0,u=n.length;if(typeof t!="number"&&t!=d){var o=u;for(t=_.createCallback(t,e,3);o--&&t(n[o],o,n);)r++}else r=t==d||e?1:t||r;return h(n,0,Pe(Be(0,u-r),u))},_.intersection=function(n){for(var r=arguments,u=r.length,o=-1,i=f(),l=-1,c=ct(),p=n?n.length:0,h=[],v=f();++o<u;){var y=r[o];
i[o]=c===t&&(y?y.length:0)>=E&&a(o?r[o]:v)}n:for(;++l<p;){var m=i[0],y=n[l];if(0>(m?e(m,y):c(v,y))){for(o=u,(m||v).push(y);--o;)if(m=i[o],0>(m?e(m,y):c(r[o],y)))continue n;h.push(y)}}for(;u--;)(m=i[u])&&g(m);return s(i),s(v),h},_.invert=yt,_.invoke=function(n,t){var e=Fe.call(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,a=Qt(typeof o=="number"?o:0);return Et(n,function(n){a[++r]=(u?t:n[t]).apply(n,e)}),a},_.keys=We,_.map=St,_.max=At,_.memoize=function(n,t){function e(){var r=e.cache,u=C+(t?t.apply(this,arguments):arguments[0]);
i[o]=c===t&&(y?y.length:0)>=E&&a(o?r[o]:v)}n:for(;++l<p;){var m=i[0],y=n[l];if(0>(m?e(m,y):c(v,y))){for(o=u,(m||v).push(y);--o;)if(m=i[o],0>(m?e(m,y):c(r[o],y)))continue n;h.push(y)}}for(;u--;)(m=i[u])&&g(m);return s(i),s(v),h},_.invert=yt,_.invoke=function(n,t){var e=Fe.call(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,a=Qt(typeof o=="number"?o:0);return Et(n,function(n){a[++r]=(u?t:n[t]).apply(n,e)}),a},_.keys=We,_.map=St,_.max=At,_.memoize=function(n,t){function e(){var r=e.cache,u=t?t.apply(this,arguments):C+arguments[0];
return me.call(r,u)?r[u]:r[u]=n.apply(this,arguments)}if(!mt(n))throw new oe;return e.cache={},e},_.merge=function(n){var t=arguments,e=2;if(!dt(n))return n;if("number"!=typeof t[2]&&(e=t.length),3<e&&"function"==typeof t[e-2])var r=Y(t[--e-1],t[e--],2);else 2<e&&"function"==typeof t[e-1]&&(r=t[--e]);for(var t=Fe.call(arguments,1,e),u=-1,o=f(),a=f();++u<e;)rt(n,t[u],r,o,a);return s(o),s(a),n},_.min=function(n,t,e){var r=1/0,o=r;if(!t&&qe(n)){e=-1;for(var a=n.length;++e<a;){var i=n[e];i<o&&(o=i)}}else t=!t&&_t(n)?u:_.createCallback(t,e,3),Xe(n,function(n,e,u){e=t(n,e,u),e<r&&(r=e,o=n)
});return o},_.omit=function(n,t,e){var r=ct(),u=typeof t=="function",o={};if(u)t=_.createCallback(t,e,3);else var a=Z(arguments,m,b,1);return nr(n,function(n,e,i){(u?!t(n,e,i):0>r(a,e))&&(o[e]=n)}),o},_.once=function(n){var t,e;if(!mt(n))throw new oe;return function(){return t?e:(t=m,e=n.apply(this,arguments),n=d,e)}},_.pairs=function(n){for(var t=-1,e=We(n),r=e.length,u=Qt(r);++t<r;){var o=e[t];u[t]=[o,n[o]]}return u},_.partial=function(n){return at(n,16,Fe.call(arguments,1))},_.partialRight=function(n){return at(n,32,d,Fe.call(arguments,1))
},_.pick=function(n,t,e){var r={};if(typeof t!="function")for(var u=-1,o=Z(arguments,m,b,1),a=dt(n)?o.length:0;++u<a;){var i=o[u];i in n&&(r[i]=n[i])}else t=_.createCallback(t,e,3),nr(n,function(n,e,u){t(n,e,u)&&(r[e]=n)});return r},_.pluck=ar,_.pull=function(n){for(var t=arguments,e=0,r=t.length,u=n?n.length:0;++e<r;)for(var o=-1,a=t[e];++o<u;)n[o]===a&&(je.call(n,o--,1),u--);return n},_.range=function(n,t,e){n=+n||0,e=typeof e=="number"?e:+e||1,t==d&&(t=n,n=0);var r=-1;t=Be(0,se((t-n)/(e||1)));

16
dist/lodash.js vendored
View File

@@ -5169,6 +5169,20 @@
* var fibonacci = _.memoize(function(n) {
* return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
* });
*
* var data = {
* 'moe': { 'name': 'moe', 'age': 40 },
* 'curly': { 'name': 'curly', 'age': 60 }
* };
*
* // modifying the result cache
* var stooge = _.memoize(function(name) { return data[name]; }, _.identity);
* stooge('curly');
* // => { 'name': 'curly', 'age': 60 }
*
* stooge.cache.curly.name = 'jerome';
* stooge('curly');
* // => { 'name': 'jerome', 'age': 60 }
*/
function memoize(func, resolver) {
if (!isFunction(func)) {
@@ -5176,7 +5190,7 @@
}
var memoized = function() {
var cache = memoized.cache,
key = keyPrefix + (resolver ? resolver.apply(this, arguments) : arguments[0]);
key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0];
return hasOwnProperty.call(cache, key)
? cache[key]

2
dist/lodash.min.js vendored
View File

@@ -32,7 +32,7 @@ return dr.apply(e,arguments)});var Qr=8==Br(C+"08")?Br:function(n,t){return Br(d
}return n},Z.bindKey=function(n,t){return ft(n,19,Dr.call(arguments,2),m,t)},Z.chain=function(n){return n=new nt(n),n.__chain__=y,n},Z.compact=function(n){for(var t=-1,r=n?n.length:0,e=[];++t<r;){var u=n[t];u&&e.push(u)}return e},Z.compose=function(){for(var n=arguments,t=n.length||1;t--;)if(!mt(n[t]))throw new ar;return function(){for(var t=arguments,r=n.length;r--;)t=[n[r].apply(this,t)];return t[0]}},Z.countBy=Gr,Z.createCallback=function(n,t,r){var e=typeof n;if(n==m||"function"==e)return rt(n,t,r);
if("object"!=e)return function(t){return t[n]};var u=Kr(n),o=u[0],a=n[o];return 1!=u.length||a!==a||_t(a)?function(t){for(var r=u.length,e=_;r--&&(e=ut(t[u[r]],n[u[r]],m,y)););return e}:function(n){return n=n[o],a===n&&(0!==a||1/a==1/n)}},Z.curry=function(n,t){return t=typeof t=="number"?t:+t||n.length,ft(n,4,m,m,m,t)},Z.debounce=Ut,Z.defaults=H,Z.defer=Vt,Z.delay=function(n,t){if(!mt(n))throw new ar;var r=Dr.call(arguments,2);return wr(function(){n.apply(g,r)},t)},Z.difference=Ft,Z.filter=xt,Z.flatten=function(n,t,r,e){return typeof t!="boolean"&&t!=m&&(e=r,r=e&&e[t]===n?g:t,t=_),r!=m&&(n=It(n,r,e)),et(n,t)
},Z.forEach=Ot,Z.forEachRight=Et,Z.forIn=x,Z.forInRight=function(n,t,r){var e=[];x(n,function(n,t){e.push(t,n)});var u=e.length;for(t=rt(t,r,3);u--&&t(e[u--],e[u],n)!==false;);return n},Z.forOwn=d,Z.forOwnRight=ht,Z.functions=gt,Z.groupBy=Hr,Z.indexBy=Jr,Z.initial=function(n,t,r){if(!n)return[];var e=0,u=n.length;if(typeof t!="number"&&t!=m){var o=u;for(t=Z.createCallback(t,r,3);o--&&t(n[o],o,n);)e++}else e=t==m||r?1:t||e;return v(n,0,Rr(Nr(0,u-e),u))},Z.intersection=function(n){for(var e=arguments,u=e.length,o=-1,i=f(),c=-1,l=lt(),v=n?n.length:0,h=[],g=f();++o<u;){var y=e[o];
i[o]=l===t&&(y?y.length:0)>=k&&a(o?e[o]:g)}n:for(;++c<v;){var m=i[0],y=n[c];if(0>(m?r(m,y):l(g,y))){for(o=u,(m||g).push(y);--o;)if(m=i[o],0>(m?r(m,y):l(e[o],y)))continue n;h.push(y)}}for(;u--;)(m=i[u])&&s(m);return p(i),p(g),h},Z.invert=yt,Z.invoke=function(n,t){var r=Dr.call(arguments,2),e=-1,u=typeof t=="function",o=n?n.length:0,a=Xt(typeof o=="number"?o:0);return Ot(n,function(n){a[++e]=(u?t:n[t]).apply(n,r)}),a},Z.keys=Kr,Z.map=It,Z.max=St,Z.memoize=function(n,t){function r(){var e=r.cache,u=j+(t?t.apply(this,arguments):arguments[0]);
i[o]=l===t&&(y?y.length:0)>=k&&a(o?e[o]:g)}n:for(;++c<v;){var m=i[0],y=n[c];if(0>(m?r(m,y):l(g,y))){for(o=u,(m||g).push(y);--o;)if(m=i[o],0>(m?r(m,y):l(e[o],y)))continue n;h.push(y)}}for(;u--;)(m=i[u])&&s(m);return p(i),p(g),h},Z.invert=yt,Z.invoke=function(n,t){var r=Dr.call(arguments,2),e=-1,u=typeof t=="function",o=n?n.length:0,a=Xt(typeof o=="number"?o:0);return Ot(n,function(n){a[++e]=(u?t:n[t]).apply(n,r)}),a},Z.keys=Kr,Z.map=It,Z.max=St,Z.memoize=function(n,t){function r(){var e=r.cache,u=t?t.apply(this,arguments):j+arguments[0];
return mr.call(e,u)?e[u]:e[u]=n.apply(this,arguments)}if(!mt(n))throw new ar;return r.cache={},r},Z.merge=function(n){var t=arguments,r=2;if(!_t(n))return n;if("number"!=typeof t[2]&&(r=t.length),3<r&&"function"==typeof t[r-2])var e=rt(t[--r-1],t[r--],2);else 2<r&&"function"==typeof t[r-1]&&(e=t[--r]);for(var t=Dr.call(arguments,1,r),u=-1,o=f(),a=f();++u<r;)ot(n,t[u],e,o,a);return p(o),p(a),n},Z.min=function(n,t,r){var e=1/0,o=e;if(!t&&Pr(n)){r=-1;for(var a=n.length;++r<a;){var i=n[r];i<o&&(o=i)}}else t=!t&&dt(n)?u:Z.createCallback(t,r,3),Ot(n,function(n,r,u){r=t(n,r,u),r<e&&(e=r,o=n)
});return o},Z.omit=function(n,t,r){var e=lt(),u=typeof t=="function",o={};if(u)t=Z.createCallback(t,r,3);else var a=et(arguments,y,_,1);return x(n,function(n,r,i){(u?!t(n,r,i):0>e(a,r))&&(o[r]=n)}),o},Z.once=function(n){var t,r;if(!mt(n))throw new ar;return function(){return t?r:(t=y,r=n.apply(this,arguments),n=m,r)}},Z.pairs=function(n){for(var t=-1,r=Kr(n),e=r.length,u=Xt(e);++t<e;){var o=r[t];u[t]=[o,n[o]]}return u},Z.partial=function(n){return ft(n,16,Dr.call(arguments,1))},Z.partialRight=function(n){return ft(n,32,m,Dr.call(arguments,1))
},Z.pick=function(n,t,r){var e={};if(typeof t!="function")for(var u=-1,o=et(arguments,y,_,1),a=_t(n)?o.length:0;++u<a;){var i=o[u];i in n&&(e[i]=n[i])}else t=Z.createCallback(t,r,3),x(n,function(n,r,u){t(n,r,u)&&(e[r]=n)});return e},Z.pluck=At,Z.pull=function(n){for(var t=arguments,r=0,e=t.length,u=n?n.length:0;++r<e;)for(var o=-1,a=t[r];++o<u;)n[o]===a&&(jr.call(n,o--,1),u--);return n},Z.range=function(n,t,r){n=+n||0,r=typeof r=="number"?r:+r||1,t==m&&(t=n,n=0);var e=-1;t=Nr(0,pr((t-n)/(r||1)));

View File

@@ -3781,6 +3781,20 @@
* var fibonacci = _.memoize(function(n) {
* return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
* });
*
* var data = {
* 'moe': { 'name': 'moe', 'age': 40 },
* 'curly': { 'name': 'curly', 'age': 60 }
* };
*
* // modifying the result cache
* var stooge = _.memoize(function(name) { return data[name]; }, _.identity);
* stooge('curly');
* // => { 'name': 'curly', 'age': 60 }
*
* stooge.cache.curly.name = 'jerome';
* stooge('curly');
* // => { 'name': 'jerome', 'age': 60 }
*/
function memoize(func, resolver) {
var cache = {};

View File

@@ -1062,7 +1062,7 @@ _.isArray(squares.value());
<!-- div -->
### <a id="_chainvalue"></a>`_.chain(value)`
<a href="#_chainvalue">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6207 "View in source") [&#x24C9;][1]
<a href="#_chainvalue">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6221 "View in source") [&#x24C9;][1]
Creates a `lodash` object that wraps the given `value`.
@@ -1095,7 +1095,7 @@ var youngest = _.chain(stooges)
<!-- div -->
### <a id="_tapvalue-interceptor"></a>`_.tap(value, interceptor)`
<a href="#_tapvalue-interceptor">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6235 "View in source") [&#x24C9;][1]
<a href="#_tapvalue-interceptor">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6249 "View in source") [&#x24C9;][1]
Invokes `interceptor` with the `value` as the first argument and then returns `value`. The purpose of this method is to "tap into" a method chain in order to perform operations on intermediate results within the chain.
@@ -1125,7 +1125,7 @@ _([1, 2, 3, 4])
<!-- div -->
### <a id="_prototypechain"></a>`_.prototype.chain()`
<a href="#_prototypechain">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6255 "View in source") [&#x24C9;][1]
<a href="#_prototypechain">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6269 "View in source") [&#x24C9;][1]
Enables method chaining on the wrapper object.
@@ -1149,7 +1149,7 @@ var sum = _([1, 2, 3])
<!-- div -->
### <a id="_prototypetostring"></a>`_.prototype.toString()`
<a href="#_prototypetostring">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6272 "View in source") [&#x24C9;][1]
<a href="#_prototypetostring">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6286 "View in source") [&#x24C9;][1]
Produces the `toString` result of the wrapped value.
@@ -1170,7 +1170,7 @@ _([1, 2, 3]).toString();
<!-- div -->
### <a id="_prototypevalueof"></a>`_.prototype.valueOf()`
<a href="#_prototypevalueof">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6289 "View in source") [&#x24C9;][1]
<a href="#_prototypevalueof">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6303 "View in source") [&#x24C9;][1]
Extracts the wrapped value.
@@ -2476,7 +2476,7 @@ _.delay(log, 1000, 'logged later');
<!-- div -->
### <a id="_memoizefunc--resolver"></a>`_.memoize(func [, resolver])`
<a href="#_memoizefunc--resolver">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5532 "View in source") [&#x24C9;][1]
<a href="#_memoizefunc--resolver">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5546 "View in source") [&#x24C9;][1]
Creates a function that memoizes the result of `func`. If `resolver` is provided it will be used to determine the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the cache key. The `func` is executed with the `this` binding of the memoized function. The result cache is exposed as the `cache` property on the memoized function.
@@ -2492,6 +2492,20 @@ Creates a function that memoizes the result of `func`. If `resolver` is provided
var fibonacci = _.memoize(function(n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
});
var data = {
'moe': { 'name': 'moe', 'age': 40 },
'curly': { 'name': 'curly', 'age': 60 }
};
// modifying the result cache
var stooge = _.memoize(function(name) { return data[name]; }, _.identity);
stooge('curly');
// => { 'name': 'curly', 'age': 60 }
stooge.cache.curly.name = 'jerome';
stooge('curly');
// => { 'name': 'jerome', 'age': 60 }
```
* * *
@@ -2502,7 +2516,7 @@ var fibonacci = _.memoize(function(n) {
<!-- div -->
### <a id="_oncefunc"></a>`_.once(func)`
<a href="#_oncefunc">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5565 "View in source") [&#x24C9;][1]
<a href="#_oncefunc">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5579 "View in source") [&#x24C9;][1]
Creates a function that is restricted to execute `func` once. Repeat calls to the function will return the value of the first call. The `func` is executed with the `this` binding of the created function.
@@ -2528,7 +2542,7 @@ initialize();
<!-- div -->
### <a id="_partialfunc--arg1-arg2-"></a>`_.partial(func [, arg1, arg2, ...])`
<a href="#_partialfunc--arg1-arg2-">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5603 "View in source") [&#x24C9;][1]
<a href="#_partialfunc--arg1-arg2-">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5617 "View in source") [&#x24C9;][1]
Creates a function that, when called, invokes `func` with any additional `partial` arguments prepended to those provided to the new function. This method is similar to `_.bind` except it does **not** alter the `this` binding.
@@ -2555,7 +2569,7 @@ hi('moe');
<!-- div -->
### <a id="_partialrightfunc--arg1-arg2-"></a>`_.partialRight(func [, arg1, arg2, ...])`
<a href="#_partialrightfunc--arg1-arg2-">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5634 "View in source") [&#x24C9;][1]
<a href="#_partialrightfunc--arg1-arg2-">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5648 "View in source") [&#x24C9;][1]
This method is like `_.partial` except that `partial` arguments are appended to those provided to the new function.
@@ -2592,7 +2606,7 @@ options.imports
<!-- div -->
### <a id="_throttlefunc-wait-options"></a>`_.throttle(func, wait, options)`
<a href="#_throttlefunc-wait-options">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5669 "View in source") [&#x24C9;][1]
<a href="#_throttlefunc-wait-options">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5683 "View in source") [&#x24C9;][1]
Creates a function that, when executed, will only call the `func` function at most once per every `wait` milliseconds. Provide an options object to indicate that `func` should be invoked on the leading and/or trailing edge of the `wait` timeout. Subsequent calls to the throttled function will return the result of the last `func` call.
@@ -2626,7 +2640,7 @@ jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
<!-- div -->
### <a id="_wrapvalue-wrapper"></a>`_.wrap(value, wrapper)`
<a href="#_wrapvalue-wrapper">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5713 "View in source") [&#x24C9;][1]
<a href="#_wrapvalue-wrapper">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5727 "View in source") [&#x24C9;][1]
Creates a function that provides `value` to the wrapper function as its first argument. Additional arguments provided to the function are appended to those provided to the wrapper function. The wrapper is executed with the `this` binding of the created function.
@@ -3801,7 +3815,7 @@ _.values({ 'one': 1, 'two': 2, 'three': 3 });
<!-- div -->
### <a id="_escapestring"></a>`_.escape(string)`
<a href="#_escapestring">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5740 "View in source") [&#x24C9;][1]
<a href="#_escapestring">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5754 "View in source") [&#x24C9;][1]
Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their corresponding HTML entities.
@@ -3825,7 +3839,7 @@ _.escape('Moe, Larry & Curly');
<!-- div -->
### <a id="_identityvalue"></a>`_.identity(value)`
<a href="#_identityvalue">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5758 "View in source") [&#x24C9;][1]
<a href="#_identityvalue">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5772 "View in source") [&#x24C9;][1]
This method returns the first argument provided to it.
@@ -3850,7 +3864,7 @@ moe === _.identity(moe);
<!-- div -->
### <a id="_mixinobject-object"></a>`_.mixin(object, object)`
<a href="#_mixinobject-object">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5785 "View in source") [&#x24C9;][1]
<a href="#_mixinobject-object">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5799 "View in source") [&#x24C9;][1]
Adds function properties of a source object to the `lodash` function and chainable wrapper.
@@ -3881,7 +3895,7 @@ _('moe').capitalize();
<!-- div -->
### <a id="_noconflict"></a>`_.noConflict()`
<a href="#_noconflict">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5823 "View in source") [&#x24C9;][1]
<a href="#_noconflict">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5837 "View in source") [&#x24C9;][1]
Reverts the '_' variable to its previous value and returns a reference to the `lodash` function.
@@ -3901,7 +3915,7 @@ var lodash = _.noConflict();
<!-- div -->
### <a id="_parseintvalue--radix"></a>`_.parseInt(value [, radix])`
<a href="#_parseintvalue--radix">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5847 "View in source") [&#x24C9;][1]
<a href="#_parseintvalue--radix">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5861 "View in source") [&#x24C9;][1]
Converts the given `value` into an integer of the specified `radix`. If `radix` is `undefined` or `0` a `radix` of `10` is used unless the `value` is a hexadecimal, in which case a `radix` of `16` is used.
@@ -3928,7 +3942,7 @@ _.parseInt('08');
<!-- div -->
### <a id="_randommin0-max1"></a>`_.random([min=0, max=1])`
<a href="#_randommin0-max1">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5871 "View in source") [&#x24C9;][1]
<a href="#_randommin0-max1">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5885 "View in source") [&#x24C9;][1]
Produces a random number between `min` and `max` *(inclusive)*. If only one argument is provided a number between `0` and the given number will be returned.
@@ -3956,7 +3970,7 @@ _.random(5);
<!-- div -->
### <a id="_resultobject-property"></a>`_.result(object, property)`
<a href="#_resultobject-property">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5915 "View in source") [&#x24C9;][1]
<a href="#_resultobject-property">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5929 "View in source") [&#x24C9;][1]
Resolves the value of `property` on `object`. If `property` is a function it will be invoked with the `this` binding of `object` and its result returned, else the property value is returned. If `object` is falsey then `undefined` is returned.
@@ -4009,7 +4023,7 @@ Create a new `lodash` function using the given `context` object.
<!-- div -->
### <a id="_templatetext-data-options"></a>`_.template(text, data, options)`
<a href="#_templatetext-data-options">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6006 "View in source") [&#x24C9;][1]
<a href="#_templatetext-data-options">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6020 "View in source") [&#x24C9;][1]
A micro-templating method that handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code.
@@ -4097,7 +4111,7 @@ fs.writeFileSync(path.join(cwd, 'jst.js'), '\
<!-- div -->
### <a id="_timesn-callback--thisarg"></a>`_.times(n, callback [, thisArg])`
<a href="#_timesn-callback--thisarg">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6131 "View in source") [&#x24C9;][1]
<a href="#_timesn-callback--thisarg">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6145 "View in source") [&#x24C9;][1]
Executes the callback `n` times, returning an array of the results of each callback execution. The callback is bound to `thisArg` and invoked with one argument; *(index)*.
@@ -4129,7 +4143,7 @@ _.times(3, function(n) { this.cast(n); }, mage);
<!-- div -->
### <a id="_unescapestring"></a>`_.unescape(string)`
<a href="#_unescapestring">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6158 "View in source") [&#x24C9;][1]
<a href="#_unescapestring">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6172 "View in source") [&#x24C9;][1]
The inverse of `_.escape` this method converts the HTML entities `&amp;`, `&lt;`, `&gt;`, `&quot;`, and `&#39;` in `string` to their corresponding characters.
@@ -4153,7 +4167,7 @@ _.unescape('Moe, Larry &amp; Curly');
<!-- div -->
### <a id="_uniqueidprefix"></a>`_.uniqueId([prefix])`
<a href="#_uniqueidprefix">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6178 "View in source") [&#x24C9;][1]
<a href="#_uniqueidprefix">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6192 "View in source") [&#x24C9;][1]
Generates a unique ID. If `prefix` is provided the ID will be appended to it.
@@ -4206,7 +4220,7 @@ A reference to the `lodash` function.
<!-- div -->
### <a id="_version"></a>`_.VERSION`
<a href="#_version">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6487 "View in source") [&#x24C9;][1]
<a href="#_version">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6501 "View in source") [&#x24C9;][1]
*(String)*: The semantic version number.

View File

@@ -5528,6 +5528,20 @@
* var fibonacci = _.memoize(function(n) {
* return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
* });
*
* var data = {
* 'moe': { 'name': 'moe', 'age': 40 },
* 'curly': { 'name': 'curly', 'age': 60 }
* };
*
* // modifying the result cache
* var stooge = _.memoize(function(name) { return data[name]; }, _.identity);
* stooge('curly');
* // => { 'name': 'curly', 'age': 60 }
*
* stooge.cache.curly.name = 'jerome';
* stooge('curly');
* // => { 'name': 'jerome', 'age': 60 }
*/
function memoize(func, resolver) {
if (!isFunction(func)) {
@@ -5535,7 +5549,7 @@
}
var memoized = function() {
var cache = memoized.cache,
key = keyPrefix + (resolver ? resolver.apply(this, arguments) : arguments[0]);
key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0];
return hasOwnProperty.call(cache, key)
? cache[key]

View File

@@ -2405,13 +2405,13 @@
(function() {
test('should expose a `cache` object on the `memoized` function', function() {
var memoized = _.memoize(_.identity);
var memoized = _.memoize(_.identity, _.identity);
memoized('x');
equal(memoized.cache.x, 'x');
var cache = memoized.cache,
key = _.keys(cache)[0];
equal(cache[key], 'x');
memoized.cache.x = 'y';
equal(memoized('x'), 'y');
});
}());