mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 01:57:50 +00:00
Cleanup _.sum.
This commit is contained in:
@@ -11309,28 +11309,25 @@
|
|||||||
* @returns {number} Returns the sum.
|
* @returns {number} Returns the sum.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* _.sum([4, 6, 2]);
|
* _.sum([4, 6]);
|
||||||
* // => 12
|
* // => 10
|
||||||
*
|
*
|
||||||
* _.sum({ 'a': 4, 'b': 6, 'c': 2 });
|
* _.sum({ 'a': 4, 'b': 6 });
|
||||||
* // => 12
|
* // => 10
|
||||||
*
|
*
|
||||||
* _.sum([]);
|
* var objects = [
|
||||||
* // => 0
|
* { 'n': 4 },
|
||||||
*
|
* { 'n': 6 }
|
||||||
* var users = [
|
|
||||||
* { 'user': 'barney', 'age': 36 },
|
|
||||||
* { 'user': 'fred', 'age': 40 }
|
|
||||||
* ];
|
* ];
|
||||||
*
|
*
|
||||||
* _.sum(users, function(chr) {
|
* _.sum(objects, function(object) {
|
||||||
* return chr.age;
|
* return object.n;
|
||||||
* });
|
* });
|
||||||
* // => 76
|
* // => 10
|
||||||
*
|
*
|
||||||
* // using the `_.property` callback shorthand
|
* // using the `_.property` callback shorthand
|
||||||
* _.sum(users, 'age');
|
* _.sum(objects, 'n');
|
||||||
* // => 76
|
* // => 10
|
||||||
*/
|
*/
|
||||||
function sum(collection, iteratee, thisArg) {
|
function sum(collection, iteratee, thisArg) {
|
||||||
if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
|
if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
|
||||||
@@ -11339,16 +11336,19 @@
|
|||||||
if (!isArray(collection)) {
|
if (!isArray(collection)) {
|
||||||
collection = toIterable(collection);
|
collection = toIterable(collection);
|
||||||
}
|
}
|
||||||
var func = getCallback();
|
var func = getCallback(),
|
||||||
if (!(func === baseCallback && iteratee == null)) {
|
noIteratee = iteratee == null;
|
||||||
|
|
||||||
|
if (!(func === baseCallback && noIteratee)) {
|
||||||
|
noIteratee = false;
|
||||||
iteratee = func(iteratee, thisArg, 3);
|
iteratee = func(iteratee, thisArg, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
var length = collection.length,
|
var length = collection.length,
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
||||||
while (length--) {
|
while (length--) {
|
||||||
result += +(iteratee == null ? collection[length] : iteratee(collection[length])) || 0;
|
var value = collection[length];
|
||||||
|
result += +(noIteratee ? value : iteratee(value)) || 0;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
27
test/test.js
27
test/test.js
@@ -13475,10 +13475,12 @@
|
|||||||
QUnit.module('lodash.sum');
|
QUnit.module('lodash.sum');
|
||||||
|
|
||||||
(function() {
|
(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() {
|
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() {
|
test('should return `0` when passing empty `array` values', 1, function() {
|
||||||
@@ -13496,7 +13498,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should iterate an object', 1, function() {
|
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() {
|
test('should iterate a string', 2, function() {
|
||||||
@@ -13514,29 +13516,22 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should support the `thisArg` argument', 1, function() {
|
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);
|
return this.floor(num);
|
||||||
}, Math);
|
}, Math);
|
||||||
|
|
||||||
strictEqual(actual, 4);
|
strictEqual(actual, 12);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with a "_.property" style `iteratee`', 2, function() {
|
test('should work with a "_.property" style `iteratee`', 2, function() {
|
||||||
var actual = _.sum(objects, 'a');
|
|
||||||
|
|
||||||
strictEqual(actual, 6);
|
|
||||||
|
|
||||||
var arrays = [[2], [3], [1]];
|
var arrays = [[2], [3], [1]];
|
||||||
actual = _.sum(arrays, 0);
|
strictEqual(_.sum(arrays, 0), 6);
|
||||||
|
strictEqual(_.sum(objects, 'a'), 6);
|
||||||
strictEqual(actual, 6);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should perform basic sum when used as an iteratee for methods like `_.map`', 1, function() {
|
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]],
|
var actual = _.map([array, object], _.sum);
|
||||||
actual = _.map(array, _.sum);
|
deepEqual(actual, [12, 6]);
|
||||||
|
|
||||||
deepEqual(actual, [6, 4]);
|
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user