Skip to content

Commit

Permalink
Implementing precision option with tests, fixes #117, fixes #132
Browse files Browse the repository at this point in the history
Adding test for invalid `precision` value
  • Loading branch information
avoidwork committed Jun 30, 2021
1 parent 8ec6429 commit 31d9a21
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 8 deletions.
12 changes: 11 additions & 1 deletion lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,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, roundingFunc;
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
Expand All @@ -67,6 +67,7 @@
num = Number(arg);
neg = num < 0;
ceil = base > 2 ? 1000 : 1024;
precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;

// Flipping a negative number to determine the size
if (neg) {
Expand All @@ -84,6 +85,10 @@

// Exceeding supported length, time to reduce & multiply
if (e > 8) {
if (precision > 0) {
precision += 8 - e;
}

e = 8;
}

Expand Down Expand Up @@ -132,6 +137,11 @@
result[0] = -result[0];
}

// Setting optional precision
if (precision > 0) {
result[0] = result[0].toPrecision(precision);
}

// Applying custom symbol
result[1] = symbols[result[1]] || result[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.

15 changes: 13 additions & 2 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
spacer,
standard,
symbols,
roundingFunc;
roundingFunc,
precision;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
Expand All @@ -86,7 +87,8 @@
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
ceil = base > 2 ? 1000 : 1024;
precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0; // Flipping a negative number to determine the size

if (neg) {
num = -num;
Expand All @@ -103,6 +105,10 @@


if (e > 8) {
if (precision > 0) {
precision += 8 - e;
}

e = 8;
}

Expand Down Expand Up @@ -149,6 +155,11 @@

if (neg) {
result[0] = -result[0];
} // Setting optional precision


if (precision > 0) {
result[0] = result[0].toPrecision(precision);
} // Applying custom symbol


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.

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

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const b = /^(b|B)$/,
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, roundingFunc;
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
Expand All @@ -54,6 +54,7 @@ function filesize (arg, descriptor = {}) {
num = Number(arg);
neg = num < 0;
ceil = base > 2 ? 1000 : 1024;
precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;

// Flipping a negative number to determine the size
if (neg) {
Expand All @@ -71,6 +72,10 @@ function filesize (arg, descriptor = {}) {

// Exceeding supported length, time to reduce & multiply
if (e > 8) {
if (precision > 0) {
precision += 8 - e;
}

e = 8;
}

Expand Down Expand Up @@ -119,6 +124,11 @@ function filesize (arg, descriptor = {}) {
result[0] = -result[0];
}

// Setting optional precision
if (precision > 0) {
result[0] = result[0].toPrecision(precision);
}

// Applying custom symbol
result[1] = symbols[result[1]] || result[1];

Expand Down
9 changes: 9 additions & 0 deletions test/filesize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,14 @@ exports.filesize = {
test.equal(filesize(1024 * 1.666, {round: 1, roundingMethod: "floor"}), "1.6 KB", "Should be '1.6 KB'");
test.equal(filesize(1024 * 1.666, {round: 1, roundingMethod: "ceil"}), "1.7 KB", "Should be '1.7 KB'");
test.done();
},
precision: function (test) {
test.expect(5);
test.equal(filesize(1024 * 1, {precision: 3}), "1.00 KB", "Should be '1.00 KB'");
test.equal(filesize(1024 * 1024 * 10.25, {precision: 3}), "10.3 MB", "Should be '10.3 MB'");
test.equal(filesize(1024 * 1024 * 10.25, {precision: "x"}), "10.25 MB", "Should be '10.25 MB'");
test.equal(filesize(1024 * 1024 * 1024, {precision: 3}), "1.00 GB", "Should be '1.00 GB'");
test.equal(filesize(Math.pow(1024, 10), {precision: 3}), "1e+6 YB", "Should be '1e+6 YB'");
test.done();
}
};

0 comments on commit 31d9a21

Please sign in to comment.