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

[feature suggestion] add roundingMethod option #131

Merged
Merged
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
@@ -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
4 changes: 4 additions & 0 deletions filesize.d.ts
Original file line number Diff line number Diff line change
@@ -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 {
10 changes: 8 additions & 2 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
@@ -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
};

/**
@@ -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");
@@ -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;
@@ -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;
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
@@ -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
@@ -57,7 +61,8 @@
separator,
spacer,
standard,
symbols;
symbols,
roundingFunc;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
@@ -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
@@ -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;
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