Allow format data util to conditionally allow padding

This commit is contained in:
John Furrow
2016-04-23 15:55:32 -07:00
parent dda0ce0e88
commit 751e305292
2 changed files with 11 additions and 4 deletions

View File

@@ -68,7 +68,7 @@ const format = {
}
},
data: (bytes, extraUnits, precision = 2) => {
data: (bytes, extraUnits, precision = 2, options = {}) => {
let kilobyte = 1024,
megabyte = kilobyte * 1024,
gigabyte = megabyte * 1024,
@@ -98,13 +98,20 @@ const format = {
value = Number(value);
if (!!value && value < 10) {
value = value.toFixed(precision);
value = Number(value.toFixed(precision));
} else if (!!value && value > 10 && value < 100) {
value = value.toFixed(precision - 1);
value = Number(value.toFixed(precision - 1));
} else if (!!value && value > 100) {
value = Math.floor(value);
}
if (options.padded === false) {
let decimal = value % 1;
if ((decimal < 0.1 && decimal >= 0) || (decimal > -0.1 && decimal <= 0)) {
value = value.toFixed(0);
}
}
if (extraUnits) {
unit += extraUnits;
}