Ensure _.reduce and _.reduceRight pass the correct number of callback arguments.

Former-commit-id: fec2d28b5a69ceb590e0ef1d8a0792b25b53c7e9
This commit is contained in:
John-David Dalton
2012-11-20 07:43:09 -08:00
parent 93636180df
commit 3df9fc6225
2 changed files with 31 additions and 8 deletions

View File

@@ -751,7 +751,7 @@
* @memberOf _ * @memberOf _
* @category Objects * @category Objects
* @param {Object} object The object to iterate over. * @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`. * @param {Mixed} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns `object`. * @returns {Object} Returns `object`.
* @example * @example
@@ -783,7 +783,7 @@
* @memberOf _ * @memberOf _
* @category Objects * @category Objects
* @param {Object} object The object to iterate over. * @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`. * @param {Mixed} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns `object`. * @returns {Object} Returns `object`.
* @example * @example
@@ -1984,7 +1984,7 @@
* @alias detect * @alias detect
* @category Collections * @category Collections
* @param {Array|Object|String} collection The collection to iterate over. * @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`. * @param {Mixed} [thisArg] The `this` binding of `callback`.
* @returns {Mixed} Returns the element that passed the callback check, * @returns {Mixed} Returns the element that passed the callback check,
* else `undefined`. * else `undefined`.
@@ -2016,7 +2016,7 @@
* @alias each * @alias each
* @category Collections * @category Collections
* @param {Array|Object|String} collection The collection to iterate over. * @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`. * @param {Mixed} [thisArg] The `this` binding of `callback`.
* @returns {Array|Object|String} Returns `collection`. * @returns {Array|Object|String} Returns `collection`.
* @example * @example
@@ -2275,7 +2275,7 @@
* @alias foldl, inject * @alias foldl, inject
* @category Collections * @category Collections
* @param {Array|Object|String} collection The collection to iterate over. * @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} [accumulator] Initial value of the accumulator.
* @param {Mixed} [thisArg] The `this` binding of `callback`. * @param {Mixed} [thisArg] The `this` binding of `callback`.
* @returns {Mixed} Returns the accumulated value. * @returns {Mixed} Returns the accumulated value.
@@ -2286,11 +2286,11 @@
*/ */
function reduce(collection, callback, accumulator, thisArg) { function reduce(collection, callback, accumulator, thisArg) {
var noaccum = arguments.length < 3; var noaccum = arguments.length < 3;
callback = createCallback(callback, thisArg); callback || (callback = identity);
forEach(collection, function(value, index, collection) { forEach(collection, function(value, index, collection) {
accumulator = noaccum accumulator = noaccum
? (noaccum = false, value) ? (noaccum = false, value)
: callback(accumulator, value, index, collection) : callback.call(thisArg, accumulator, value, index, collection)
}); });
return accumulator; return accumulator;
} }
@@ -2303,7 +2303,7 @@
* @alias foldr * @alias foldr
* @category Collections * @category Collections
* @param {Array|Object|String} collection The collection to iterate over. * @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} [accumulator] Initial value of the accumulator.
* @param {Mixed} [thisArg] The `this` binding of `callback`. * @param {Mixed} [thisArg] The `this` binding of `callback`.
* @returns {Mixed} Returns the accumulated value. * @returns {Mixed} Returns the accumulated value.
@@ -2324,6 +2324,7 @@
} else if (noCharByIndex && isString(collection)) { } else if (noCharByIndex && isString(collection)) {
iteratee = collection.split(''); iteratee = collection.split('');
} }
callback || (callback = identity);
forEach(collection, function(value, index, collection) { forEach(collection, function(value, index, collection) {
index = props ? props[--length] : --length; index = props ? props[--length] : --length;
accumulator = noaccum accumulator = noaccum

View File

@@ -1375,6 +1375,17 @@
QUnit.module('lodash.reduce'); QUnit.module('lodash.reduce');
(function() { (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({ _.each({
'literal': 'abc', 'literal': 'abc',
'object': Object('abc') 'object': Object('abc')
@@ -1399,6 +1410,17 @@
QUnit.module('lodash.reduceRight'); QUnit.module('lodash.reduceRight');
(function() { (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() { test('should pass the correct `callback` arguments when iterating an object', function() {
var args, var args,
object = { 'a': 1, 'b': 2 }, object = { 'a': 1, 'b': 2 },