Skip to content

Commit

Permalink
add roundingMethod option
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoto-hitachi committed Apr 21, 2021
1 parent e1448a6 commit f2acf64
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ _*(object)*_ Dictionary of SI/JEDEC/IEC symbols to replace for localization, def
### unix
_*(boolean)*_ Enables unix style human readable output, e.g `ls -lh`, default is `false`

### roundingMethod
_*(string)*_ Rounding method, can be `round`, `floor`, or `ceil`, default is `round`

## Examples

```javascript
Expand Down
4 changes: 4 additions & 0 deletions filesize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ declare namespace Filesize {
* Enables unix style human readable output, e.g ls -lh, default is false
*/
unix?: boolean;
/**
* Rounding method, can be round, floor, or ceil, default is round
*/
roundingMethod?: "round" | "floor" | "ceil";
}

interface Filesize {
Expand Down
10 changes: 8 additions & 2 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
fullform = {
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
},
roundingFuncs = {
floor: Math.floor,
ceil: Math.ceil
};

/**
Expand All @@ -38,7 +42,7 @@
function filesize (arg, descriptor = {}) {
let result = [],
val = 0,
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols;
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
Expand All @@ -59,6 +63,7 @@
full = descriptor.fullform === true;
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
num = Number(arg);
neg = num < 0;
ceil = base > 2 ? 1000 : 1024;
Expand Down Expand Up @@ -102,7 +107,8 @@
}
}

result[0] = Number(val.toFixed(e > 0 ? round : 0));
const p = Math.pow(10, e > 0 ? round : 0);
result[0] = roundingFunc(val * p) / p;

if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
result[0] = 1;
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.

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

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
fullform = {
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
},
roundingFuncs = {
floor: Math.floor,
ceil: Math.ceil
};
/**
* filesize
Expand Down Expand Up @@ -57,7 +61,8 @@
separator,
spacer,
standard,
symbols;
symbols,
roundingFunc;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
Expand All @@ -78,6 +83,7 @@
full = descriptor.fullform === true;
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
num = Number(arg);
neg = num < 0;
ceil = base > 2 ? 1000 : 1024; // Flipping a negative number to determine the size
Expand Down Expand Up @@ -120,7 +126,8 @@
}
}

result[0] = Number(val.toFixed(e > 0 ? round : 0));
var p = Math.pow(10, e > 0 ? round : 0);
result[0] = roundingFunc(val * p) / p;

if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
result[0] = 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/filesize.min.js

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

Loading

0 comments on commit f2acf64

Please sign in to comment.