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 = {};