Add support for _.sum to take iteratee.

This commit is contained in:
qsona
2015-03-16 00:47:54 +09:00
committed by jdalton
parent 306ef8addf
commit 03478afea4
2 changed files with 76 additions and 2 deletions

View File

@@ -11304,6 +11304,8 @@
* @memberOf _
* @category Math
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [iteratee] The function invoked per iteration.
* @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {number} Returns the sum.
* @example
*
@@ -11312,16 +11314,41 @@
*
* _.sum({ 'a': 4, 'b': 6, 'c': 2 });
* // => 12
*
* _.sum([]);
* // => 0
*
* var users = [
* { 'user': 'barney', 'age': 36 },
* { 'user': 'fred', 'age': 40 }
* ];
*
* _.sum(users, function(chr) {
* return chr.age;
* });
* // => 76
*
* // using the `_.property` callback shorthand
* _.sum(users, 'age');
* // => 76
*/
function sum(collection) {
function sum(collection, iteratee, thisArg) {
if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
iteratee = null;
}
if (!isArray(collection)) {
collection = toIterable(collection);
}
var func = getCallback();
if (!(func === baseCallback && iteratee == null)) {
iteratee = func(iteratee, thisArg, 3);
}
var length = collection.length,
result = 0;
while (length--) {
result += +collection[length] || 0;
result += +(iteratee == null ? collection[length] : iteratee(collection[length])) || 0;
}
return result;
}