From 172cc1ffea213ee1fceec9f76cd8f20020acae29 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 16 Dec 2013 02:34:20 -0800 Subject: [PATCH] Add `thisArg` to `_.tap`. --- dist/lodash.compat.js | 17 +++++++++-------- dist/lodash.compat.min.js | 2 +- dist/lodash.js | 17 +++++++++-------- dist/lodash.min.js | 2 +- lodash.js | 13 +++++++------ test/test.js | 26 +++++++++++++++++++++----- 6 files changed, 48 insertions(+), 29 deletions(-) diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index e97c6ce50..261e483e9 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -2955,7 +2955,7 @@ * per iteration. If a property name or object is provided it will be used * to create a "_.pluck" or "_.where" style callback, respectively. * @param {*} [thisArg] The `this` binding of `callback`. - * @returns {Array} Returns a new object with values of the results of each `callback` execution. + * @returns {Object} Returns a new object with values of the results of each `callback` execution. * @example * * _.mapValues({ 'a': 1, 'b': 2, 'c': 3} , function(num) { return num * 3; }); @@ -4190,7 +4190,7 @@ * @param {number} [n] The number of elements to sample. * @param- {Object} [guard] Allows working with functions like `_.map` * without using their `index` arguments as `n`. - * @returns {Array} Returns the random sample(s) of `collection`. + * @returns {*} Returns the random sample(s) of `collection`. * @example * * _.sample([1, 2, 3, 4]); @@ -6825,16 +6825,17 @@ } /** - * 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. + * This method invokes `interceptor` and returns `value`. The interceptor is + * bound to `thisArg` and invoked with one argument; (value). The purpose of + * this method is to "tap into" a method chain in order to perform operations + * on intermediate results within the chain. * * @static * @memberOf _ * @category Chaining * @param {*} value The value to provide to `interceptor`. * @param {Function} interceptor The function to invoke. + * @param {*} [thisArg] The `this` binding of `interceptor`. * @returns {*} Returns `value`. * @example * @@ -6844,8 +6845,8 @@ * .value(); * // => [3, 2, 1] */ - function tap(value, interceptor) { - interceptor(value); + function tap(value, interceptor, thisArg) { + interceptor.call(thisArg, value); return value; } diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index 7a723dde8..765db0c3b 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -44,7 +44,7 @@ if(typeof t!="number"&&null!=t){var o=u;for(t=v.createCallback(t,r,3);o--&&t(n[o });return o},v.omit=function(n,t,r){var e={};if(typeof t!="function"){var u=[];Zr(n,function(n,t){u.push(t)});for(var u=rt(u,ot(arguments,true,false,1)),o=-1,a=u.length;++o [3, 2, 1] */ - function tap(value, interceptor) { - interceptor(value); + function tap(value, interceptor, thisArg) { + interceptor.call(thisArg, value); return value; } diff --git a/dist/lodash.min.js b/dist/lodash.min.js index f07cde8bc..0aa5a8439 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -39,7 +39,7 @@ return t=d.createCallback(t,e,3),bt(n,function(n,e,u){r[e]=t(n,e,u)}),r},d.max=$ return l(o),l(i),n},d.min=function(n,t,e){var u=1/0,o=u;if(typeof t!="function"&&e&&e[t]===n&&(t=null),null==t&&qe(n)){e=-1;for(var i=n.length;++e [3, 2, 1] */ - function tap(value, interceptor) { - interceptor(value); + function tap(value, interceptor, thisArg) { + interceptor.call(thisArg, value); return value; } diff --git a/test/test.js b/test/test.js index 703697082..d3c76e64c 100644 --- a/test/test.js +++ b/test/test.js @@ -6994,14 +6994,15 @@ (function() { test('should intercept and return the given value', 2, function() { if (!isNpm) { - var intercepted; + var intercepted, + array = [1, 2, 3]; - var actual = _.tap('a', function(value) { + var actual = _.tap(array, function(value) { intercepted = value; }); - equal(actual, 'a'); - equal(intercepted, 'a'); + strictEqual(actual, array); + strictEqual(intercepted, array); } else { skipTest(2); @@ -7011,7 +7012,7 @@ test('should return intercept unwrapped values and return wrapped values when chaining', 2, function() { if (!isNpm) { var intercepted, - array = [1, 2, 3, 4]; + array = [1, 2, 3]; var actual = _(array).tap(function(value) { intercepted = value; @@ -7025,6 +7026,21 @@ skipTest(2); } }); + + test('should support the `thisArg` argument', 1, function() { + if (!isNpm) { + var array = [1, 2]; + + var actual = _(array.slice()).tap(function(value) { + value.push(this[0]); + }, array); + + deepEqual(actual.value(), [1, 2, 1]); + } + else { + skipTest(); + } + }); }()); /*--------------------------------------------------------------------------*/