Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sum function to number formatters #145

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions formatters/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,25 @@ function div (d, value) {
return d;
}

/**
* Sum of array
*
* @version 3.2.3
*
* @param {key} key if object array
* @return {Number} Result
*/
function sum (d, key) {
if (Array.isArray(d) == true && d.length > 0) {
if (typeof d[0] == "object") {
return d.reduce(function(total, value) { return total + parseFloat(value[key]) }, 0);
} else if ( typeof d[0] == "number") {
return d.reduce(function(total, value) { return total + parseFloat(value) }, 0);
}
}
return d;
}

module.exports = {
formatN : formatN,
formatC : formatC,
Expand All @@ -265,6 +284,7 @@ module.exports = {
sub : sub,
mul : mul,
div : div,
sum : sum,

/**
* Converts a number to an INT
Expand Down