diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index 68a5eb99d..07ee57409 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -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] diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index 0b424baf0..f06b3def0 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -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&&a(o?r[o]:v)}n:for(;++l(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(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),3r(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 { '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] diff --git a/dist/lodash.min.js b/dist/lodash.min.js index 75a880443..444ead81d 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -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=k&&a(o?e[o]:g)}n:for(;++c(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(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),3e(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 { 'name': 'curly', 'age': 60 } + * + * stooge.cache.curly.name = 'jerome'; + * stooge('curly'); + * // => { 'name': 'jerome', 'age': 60 } */ function memoize(func, resolver) { var cache = {}; diff --git a/doc/README.md b/doc/README.md index c3c686206..fdaf441fc 100644 --- a/doc/README.md +++ b/doc/README.md @@ -1062,7 +1062,7 @@ _.isArray(squares.value()); ### `_.chain(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6207 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6221 "View in source") [Ⓣ][1] Creates a `lodash` object that wraps the given `value`. @@ -1095,7 +1095,7 @@ var youngest = _.chain(stooges) ### `_.tap(value, interceptor)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6235 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6249 "View in source") [Ⓣ][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]) ### `_.prototype.chain()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6255 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6269 "View in source") [Ⓣ][1] Enables method chaining on the wrapper object. @@ -1149,7 +1149,7 @@ var sum = _([1, 2, 3]) ### `_.prototype.toString()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6272 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6286 "View in source") [Ⓣ][1] Produces the `toString` result of the wrapped value. @@ -1170,7 +1170,7 @@ _([1, 2, 3]).toString(); ### `_.prototype.valueOf()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6289 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6303 "View in source") [Ⓣ][1] Extracts the wrapped value. @@ -2476,7 +2476,7 @@ _.delay(log, 1000, 'logged later'); ### `_.memoize(func [, resolver])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5532 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5546 "View in source") [Ⓣ][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) { ### `_.once(func)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5565 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5579 "View in source") [Ⓣ][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(); ### `_.partial(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5603 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5617 "View in source") [Ⓣ][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'); ### `_.partialRight(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5634 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5648 "View in source") [Ⓣ][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 ### `_.throttle(func, wait, options)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5669 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5683 "View in source") [Ⓣ][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, { ### `_.wrap(value, wrapper)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5713 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5727 "View in source") [Ⓣ][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 }); ### `_.escape(string)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5740 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5754 "View in source") [Ⓣ][1] Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their corresponding HTML entities. @@ -3825,7 +3839,7 @@ _.escape('Moe, Larry & Curly'); ### `_.identity(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5758 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5772 "View in source") [Ⓣ][1] This method returns the first argument provided to it. @@ -3850,7 +3864,7 @@ moe === _.identity(moe); ### `_.mixin(object, object)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5785 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5799 "View in source") [Ⓣ][1] Adds function properties of a source object to the `lodash` function and chainable wrapper. @@ -3881,7 +3895,7 @@ _('moe').capitalize(); ### `_.noConflict()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5823 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5837 "View in source") [Ⓣ][1] Reverts the '_' variable to its previous value and returns a reference to the `lodash` function. @@ -3901,7 +3915,7 @@ var lodash = _.noConflict(); ### `_.parseInt(value [, radix])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5847 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5861 "View in source") [Ⓣ][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'); ### `_.random([min=0, max=1])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5871 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5885 "View in source") [Ⓣ][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); ### `_.result(object, property)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5915 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5929 "View in source") [Ⓣ][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. ### `_.template(text, data, options)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6006 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6020 "View in source") [Ⓣ][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'), '\ ### `_.times(n, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6131 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6145 "View in source") [Ⓣ][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); ### `_.unescape(string)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6158 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6172 "View in source") [Ⓣ][1] The inverse of `_.escape` this method converts the HTML entities `&`, `<`, `>`, `"`, and `'` in `string` to their corresponding characters. @@ -4153,7 +4167,7 @@ _.unescape('Moe, Larry & Curly'); ### `_.uniqueId([prefix])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6178 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6192 "View in source") [Ⓣ][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. ### `_.VERSION` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6487 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6501 "View in source") [Ⓣ][1] *(String)*: The semantic version number. diff --git a/lodash.js b/lodash.js index f62f0a7dc..0d83a560a 100644 --- a/lodash.js +++ b/lodash.js @@ -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] diff --git a/test/test.js b/test/test.js index b59df6bc9..c4de32946 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); }); }());