28 lines
702 B
JavaScript
28 lines
702 B
JavaScript
|
'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));
|
||
|
};
|