Skip to content

Commit

Permalink
Adding pad optional setting to pad the ending of decimal place, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Apr 13, 2021
1 parent e6ba9f5 commit 0a78178
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ _*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), de
### round
_*(number)*_ Decimal place, default is `2`

### pad
_*(boolean)*_ Decimal place end padding, default is `false`

### separator
_*(string)*_ Decimal separator character, default is `.`

Expand Down
13 changes: 12 additions & 1 deletion lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@
function filesize (arg, descriptor = {}) {
let result = [],
val = 0,
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, round, unix, separator, spacer, standard, symbols;
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, unix, separator, spacer, standard, symbols;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
}

bits = descriptor.bits === true;
unix = descriptor.unix === true;
pad = descriptor.pad === true;
base = descriptor.base || 2;
round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
Expand Down Expand Up @@ -149,6 +150,16 @@
return {value: result[0], symbol: result[1], exponent: e};
}

if (pad && Number.isInteger(result[0]) === false && round > 0) {
const x = separator || ".",
tmp = result[0].toString().split(x),
s = tmp[1] || "",
l = s.length,
n = round - l;

result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`;
}

return result.join(spacer);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/filesize.es6.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0a78178

Please sign in to comment.