Skip to content

Commit 06d754a

Browse files
authored
Merge pull request #127 from avoidwork/pad
Adding `pad` optional setting to pad the ending of decimal place
2 parents e6ba9f5 + 0a78178 commit 06d754a

File tree

9 files changed

+44
-7
lines changed

9 files changed

+44
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ _*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), de
3535
### round
3636
_*(number)*_ Decimal place, default is `2`
3737

38+
### pad
39+
_*(boolean)*_ Decimal place end padding, default is `false`
40+
3841
### separator
3942
_*(string)*_ Decimal separator character, default is `.`
4043

lib/filesize.es6.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@
3838
function filesize (arg, descriptor = {}) {
3939
let result = [],
4040
val = 0,
41-
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, round, unix, separator, spacer, standard, symbols;
41+
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, unix, separator, spacer, standard, symbols;
4242

4343
if (isNaN(arg)) {
4444
throw new TypeError("Invalid number");
4545
}
4646

4747
bits = descriptor.bits === true;
4848
unix = descriptor.unix === true;
49+
pad = descriptor.pad === true;
4950
base = descriptor.base || 2;
5051
round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
5152
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
@@ -149,6 +150,16 @@
149150
return {value: result[0], symbol: result[1], exponent: e};
150151
}
151152

153+
if (pad && Number.isInteger(result[0]) === false && round > 0) {
154+
const x = separator || ".",
155+
tmp = result[0].toString().split(x),
156+
s = tmp[1] || "",
157+
l = s.length,
158+
n = round - l;
159+
160+
result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`;
161+
}
162+
152163
return result.join(spacer);
153164
}
154165

lib/filesize.es6.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)