Simplify _.sum.

This commit is contained in:
jdalton
2015-06-18 07:45:03 -07:00
parent 535eec5366
commit 4bcb99a6b9

View File

@@ -1606,18 +1606,20 @@
}
/**
* A specialized version of `_.sum` for arrays without support for iteratees.
* A specialized version of `_.sum` for arrays without support for callback
* shorthands and `this` binding..
*
* @private
* @param {Array} array The array to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {number} Returns the sum.
*/
function arraySum(array) {
function arraySum(array, iteratee) {
var length = array.length,
result = 0;
while (length--) {
result += +array[length] || 0;
result += +iteratee(array[length]) || 0;
}
return result;
}
@@ -11948,15 +11950,9 @@
if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
iteratee = undefined;
}
var callback = getCallback(),
noIteratee = iteratee == null;
if (!(noIteratee && callback === baseCallback)) {
noIteratee = false;
iteratee = callback(iteratee, thisArg, 3);
}
return noIteratee
? arraySum(isArray(collection) ? collection : toIterable(collection))
iteratee = getCallback(iteratee, thisArg, 3);
return iteratee.length == 1
? arraySum(isArray(collection) ? collection : toIterable(collection), iteratee)
: baseSum(collection, iteratee);
}