From 3df9fc6225b60f12b5258b7196a55990e5b67c12 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 20 Nov 2012 07:43:09 -0800 Subject: [PATCH] Ensure `_.reduce` and `_.reduceRight` pass the correct number of `callback` arguments. Former-commit-id: fec2d28b5a69ceb590e0ef1d8a0792b25b53c7e9 --- lodash.js | 17 +++++++++-------- test/test.js | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lodash.js b/lodash.js index 247f2bb36..57918f18b 100644 --- a/lodash.js +++ b/lodash.js @@ -751,7 +751,7 @@ * @memberOf _ * @category Objects * @param {Object} object The object to iterate over. - * @param {Function} callback The function called per iteration. + * @param {Function} [callback=identity] The function called per iteration. * @param {Mixed} [thisArg] The `this` binding of `callback`. * @returns {Object} Returns `object`. * @example @@ -783,7 +783,7 @@ * @memberOf _ * @category Objects * @param {Object} object The object to iterate over. - * @param {Function} callback The function called per iteration. + * @param {Function} [callback=identity] The function called per iteration. * @param {Mixed} [thisArg] The `this` binding of `callback`. * @returns {Object} Returns `object`. * @example @@ -1984,7 +1984,7 @@ * @alias detect * @category Collections * @param {Array|Object|String} collection The collection to iterate over. - * @param {Function} callback The function called per iteration. + * @param {Function} [callback=identity] The function called per iteration. * @param {Mixed} [thisArg] The `this` binding of `callback`. * @returns {Mixed} Returns the element that passed the callback check, * else `undefined`. @@ -2016,7 +2016,7 @@ * @alias each * @category Collections * @param {Array|Object|String} collection The collection to iterate over. - * @param {Function} callback The function called per iteration. + * @param {Function} [callback=identity] The function called per iteration. * @param {Mixed} [thisArg] The `this` binding of `callback`. * @returns {Array|Object|String} Returns `collection`. * @example @@ -2275,7 +2275,7 @@ * @alias foldl, inject * @category Collections * @param {Array|Object|String} collection The collection to iterate over. - * @param {Function} callback The function called per iteration. + * @param {Function} [callback=identity] The function called per iteration. * @param {Mixed} [accumulator] Initial value of the accumulator. * @param {Mixed} [thisArg] The `this` binding of `callback`. * @returns {Mixed} Returns the accumulated value. @@ -2286,11 +2286,11 @@ */ function reduce(collection, callback, accumulator, thisArg) { var noaccum = arguments.length < 3; - callback = createCallback(callback, thisArg); + callback || (callback = identity); forEach(collection, function(value, index, collection) { accumulator = noaccum ? (noaccum = false, value) - : callback(accumulator, value, index, collection) + : callback.call(thisArg, accumulator, value, index, collection) }); return accumulator; } @@ -2303,7 +2303,7 @@ * @alias foldr * @category Collections * @param {Array|Object|String} collection The collection to iterate over. - * @param {Function} callback The function called per iteration. + * @param {Function} [callback=identity] The function called per iteration. * @param {Mixed} [accumulator] Initial value of the accumulator. * @param {Mixed} [thisArg] The `this` binding of `callback`. * @returns {Mixed} Returns the accumulated value. @@ -2324,6 +2324,7 @@ } else if (noCharByIndex && isString(collection)) { iteratee = collection.split(''); } + callback || (callback = identity); forEach(collection, function(value, index, collection) { index = props ? props[--length] : --length; accumulator = noaccum diff --git a/test/test.js b/test/test.js index bf474f2aa..b73b24bed 100644 --- a/test/test.js +++ b/test/test.js @@ -1375,6 +1375,17 @@ QUnit.module('lodash.reduce'); (function() { + test('should pass the correct `callback` arguments', function() { + var args, + array = [1, 2, 3]; + + _.reduce(array, function() { + args || (args = slice.call(arguments)); + }); + + deepEqual(args, [1, 2, 1, array]); + }); + _.each({ 'literal': 'abc', 'object': Object('abc') @@ -1399,6 +1410,17 @@ QUnit.module('lodash.reduceRight'); (function() { + test('should pass the correct `callback` arguments when iterating an array', function() { + var args, + array = [1, 2, 3]; + + _.reduceRight(array, function() { + args || (args = slice.call(arguments)); + }); + + deepEqual(args, [3, 2, 1, array]); + }); + test('should pass the correct `callback` arguments when iterating an object', function() { var args, object = { 'a': 1, 'b': 2 },