Cleanup _.sum.

This commit is contained in:
jdalton
2015-03-15 17:00:35 -07:00
parent 519eb424e7
commit c5902b3144
2 changed files with 30 additions and 35 deletions

View File

@@ -11309,28 +11309,25 @@
* @returns {number} Returns the sum.
* @example
*
* _.sum([4, 6, 2]);
* // => 12
* _.sum([4, 6]);
* // => 10
*
* _.sum({ 'a': 4, 'b': 6, 'c': 2 });
* // => 12
* _.sum({ 'a': 4, 'b': 6 });
* // => 10
*
* _.sum([]);
* // => 0
*
* var users = [
* { 'user': 'barney', 'age': 36 },
* { 'user': 'fred', 'age': 40 }
* var objects = [
* { 'n': 4 },
* { 'n': 6 }
* ];
*
* _.sum(users, function(chr) {
* return chr.age;
* _.sum(objects, function(object) {
* return object.n;
* });
* // => 76
* // => 10
*
* // using the `_.property` callback shorthand
* _.sum(users, 'age');
* // => 76
* _.sum(objects, 'n');
* // => 10
*/
function sum(collection, iteratee, thisArg) {
if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
@@ -11339,16 +11336,19 @@
if (!isArray(collection)) {
collection = toIterable(collection);
}
var func = getCallback();
if (!(func === baseCallback && iteratee == null)) {
var func = getCallback(),
noIteratee = iteratee == null;
if (!(func === baseCallback && noIteratee)) {
noIteratee = false;
iteratee = func(iteratee, thisArg, 3);
}
var length = collection.length,
result = 0;
while (length--) {
result += +(iteratee == null ? collection[length] : iteratee(collection[length])) || 0;
var value = collection[length];
result += +(noIteratee ? value : iteratee(value)) || 0;
}
return result;
}

View File

@@ -13475,10 +13475,12 @@
QUnit.module('lodash.sum');
(function() {
var objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }];
var array = [6, 4, 2],
object = { 'a': 2, 'b': 3, 'c': 1 },
objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }];
test('should return the sum of an array of numbers', 1, function() {
strictEqual(_.sum([6, 4, 2]), 12);
strictEqual(_.sum(array), 12);
});
test('should return `0` when passing empty `array` values', 1, function() {
@@ -13496,7 +13498,7 @@
});
test('should iterate an object', 1, function() {
strictEqual(_.sum({ 'a': 1, 'b': 2, 'c': 3 }), 6);
strictEqual(_.sum(object), 6);
});
test('should iterate a string', 2, function() {
@@ -13514,29 +13516,22 @@
});
test('should support the `thisArg` argument', 1, function() {
var actual = _.sum([1, 2.5, 1.5], function(num) {
var actual = _.sum([6.8, 4.5, 2.6], function(num) {
return this.floor(num);
}, Math);
strictEqual(actual, 4);
strictEqual(actual, 12);
});
test('should work with a "_.property" style `iteratee`', 2, function() {
var actual = _.sum(objects, 'a');
strictEqual(actual, 6);
var arrays = [[2], [3], [1]];
actual = _.sum(arrays, 0);
strictEqual(actual, 6);
strictEqual(_.sum(arrays, 0), 6);
strictEqual(_.sum(objects, 'a'), 6);
});
test('should perform basic sum when used as an iteratee for methods like `_.map`', 1, function() {
var array = [{ 'a': 1, 'b': 2, 'c': 3 }, [1, 2, 1]],
actual = _.map(array, _.sum);
deepEqual(actual, [6, 4]);
var actual = _.map([array, object], _.sum);
deepEqual(actual, [12, 6]);
});
}());