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

Adding pad optional setting to pad the ending of decimal place #127

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
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
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