mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 23:57:49 +00:00
Ensure _.reduce and _.reduceRight pass the correct number of callback arguments.
Former-commit-id: fec2d28b5a69ceb590e0ef1d8a0792b25b53c7e9
This commit is contained in:
17
lodash.js
17
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
|
||||
|
||||
22
test/test.js
22
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 },
|
||||
|
||||
Reference in New Issue
Block a user