-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathsum.js
More file actions
22 lines (21 loc) · 868 Bytes
/
sum.js
File metadata and controls
22 lines (21 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import isArrayLike from 'underscore/modules/_isArrayLike.js';
import values from 'underscore/modules/values.js';
import 'underscore/modules/iteratee.js';
import _ from 'underscore/modules/underscore.js';
import find from 'underscore/modules/find.js';
// Return the sum of elements (or element-based computation).
export default function sum(collection, iteratee, context) {
var result = 0;
if (iteratee == null || typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') {
collection = isArrayLike(collection) ? collection : values(collection);
for (var i = 0, length = collection.length; i < length; i++) {
result += collection[i];
}
} else {
iteratee = _.iteratee(iteratee, context);
find(collection, function(v, index, list) {
result += iteratee(v, index, list);;
});
}
return result;
}