You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
702 B
28 lines
702 B
4 years ago
|
'use strict';
|
||
|
|
||
|
var values = require('../helpers/values');
|
||
|
|
||
|
var _require = require('../helpers/is'),
|
||
|
isFunction = _require.isFunction;
|
||
|
|
||
|
module.exports = function sum(key) {
|
||
|
var items = values(this.items);
|
||
|
|
||
|
var total = 0;
|
||
|
|
||
|
if (key === undefined) {
|
||
|
for (var i = 0, length = items.length; i < length; i += 1) {
|
||
|
total += parseFloat(items[i]);
|
||
|
}
|
||
|
} else if (isFunction(key)) {
|
||
|
for (var _i = 0, _length = items.length; _i < _length; _i += 1) {
|
||
|
total += parseFloat(key(items[_i]));
|
||
|
}
|
||
|
} else {
|
||
|
for (var _i2 = 0, _length2 = items.length; _i2 < _length2; _i2 += 1) {
|
||
|
total += parseFloat(items[_i2][key]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return parseFloat(total.toPrecision(12));
|
||
|
};
|