Skip to content

Commit 4d37cc0

Browse files
committed
Build & updating tests
1 parent 10afe44 commit 4d37cc0

File tree

8 files changed

+50
-22
lines changed

8 files changed

+50
-22
lines changed

dist/filesize.cjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ function filesize (arg, {
232232
return e;
233233
}
234234

235-
// Use pre-computed lookup tables instead of Math.pow
235+
// Use pre-computed lookup tables (e is always <= 8, arrays have 9 elements)
236236
let d;
237237
if (isDecimal) {
238-
d = e < DECIMAL_POWERS.length ? DECIMAL_POWERS[e] : Math.pow(1000, e);
238+
d = DECIMAL_POWERS[e];
239239
} else {
240-
d = e < BINARY_POWERS.length ? BINARY_POWERS[e] : Math.pow(2, e * 10);
240+
d = BINARY_POWERS[e];
241241
}
242242

243243
val = num / d;
@@ -266,11 +266,11 @@ function filesize (arg, {
266266

267267
if (result[0].includes(E) && e < 8) {
268268
e++;
269-
// Recalculate with new exponent
269+
// Recalculate with new exponent (e is always <= 8)
270270
if (isDecimal) {
271-
d = e < DECIMAL_POWERS.length ? DECIMAL_POWERS[e] : Math.pow(1000, e);
271+
d = DECIMAL_POWERS[e];
272272
} else {
273-
d = e < BINARY_POWERS.length ? BINARY_POWERS[e] : Math.pow(2, e * 10);
273+
d = BINARY_POWERS[e];
274274
}
275275
val = num / d;
276276
result[0] = (p === 1 ? roundingFunc(val) : roundingFunc(val * p) / p).toPrecision(precision);

dist/filesize.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@ function filesize (arg, {
228228
return e;
229229
}
230230

231-
// Use pre-computed lookup tables instead of Math.pow
231+
// Use pre-computed lookup tables (e is always <= 8, arrays have 9 elements)
232232
let d;
233233
if (isDecimal) {
234-
d = e < DECIMAL_POWERS.length ? DECIMAL_POWERS[e] : Math.pow(1000, e);
234+
d = DECIMAL_POWERS[e];
235235
} else {
236-
d = e < BINARY_POWERS.length ? BINARY_POWERS[e] : Math.pow(2, e * 10);
236+
d = BINARY_POWERS[e];
237237
}
238238

239239
val = num / d;
@@ -262,11 +262,11 @@ function filesize (arg, {
262262

263263
if (result[0].includes(E) && e < 8) {
264264
e++;
265-
// Recalculate with new exponent
265+
// Recalculate with new exponent (e is always <= 8)
266266
if (isDecimal) {
267-
d = e < DECIMAL_POWERS.length ? DECIMAL_POWERS[e] : Math.pow(1000, e);
267+
d = DECIMAL_POWERS[e];
268268
} else {
269-
d = e < BINARY_POWERS.length ? BINARY_POWERS[e] : Math.pow(2, e * 10);
269+
d = BINARY_POWERS[e];
270270
}
271271
val = num / d;
272272
result[0] = (p === 1 ? roundingFunc(val) : roundingFunc(val * p) / p).toPrecision(precision);

dist/filesize.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.

dist/filesize.min.js.map

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

dist/filesize.umd.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@ function filesize (arg, {
228228
return e;
229229
}
230230

231-
// Use pre-computed lookup tables instead of Math.pow
231+
// Use pre-computed lookup tables (e is always <= 8, arrays have 9 elements)
232232
let d;
233233
if (isDecimal) {
234-
d = e < DECIMAL_POWERS.length ? DECIMAL_POWERS[e] : Math.pow(1000, e);
234+
d = DECIMAL_POWERS[e];
235235
} else {
236-
d = e < BINARY_POWERS.length ? BINARY_POWERS[e] : Math.pow(2, e * 10);
236+
d = BINARY_POWERS[e];
237237
}
238238

239239
val = num / d;
@@ -262,11 +262,11 @@ function filesize (arg, {
262262

263263
if (result[0].includes(E) && e < 8) {
264264
e++;
265-
// Recalculate with new exponent
265+
// Recalculate with new exponent (e is always <= 8)
266266
if (isDecimal) {
267-
d = e < DECIMAL_POWERS.length ? DECIMAL_POWERS[e] : Math.pow(1000, e);
267+
d = DECIMAL_POWERS[e];
268268
} else {
269-
d = e < BINARY_POWERS.length ? BINARY_POWERS[e] : Math.pow(2, e * 10);
269+
d = BINARY_POWERS[e];
270270
}
271271
val = num / d;
272272
result[0] = (p === 1 ? roundingFunc(val) : roundingFunc(val * p) / p).toPrecision(precision);

dist/filesize.umd.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.

dist/filesize.umd.min.js.map

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

tests/unit/filesize.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,34 @@ describe('filesize', () => {
452452
assert.ok(typeof result === 'string');
453453
});
454454

455+
it('should cover p===1 branch in precision recalculation', () => {
456+
// Try to force the p===1 branch in the precision recalculation
457+
// This needs a very specific edge case where scientific notation is produced
458+
// and after increment e=0 or round=0 makes p=1
459+
const result = filesize(0.0001, { precision: 20, round: 0 });
460+
assert.ok(typeof result === 'string');
461+
});
462+
463+
it('should trigger precision recalculation with bytes and round=0', () => {
464+
// Another attempt: very small number that might produce scientific notation in bytes
465+
const result = filesize(0.000000000000001, { precision: 30, round: 0 });
466+
assert.ok(typeof result === 'string');
467+
});
468+
469+
it('should cover p===1 branch with input=1 and extreme precision', () => {
470+
// When input=1, e=0, so p=1. High precision might trigger scientific notation
471+
// and the recalculation with p===1 branch
472+
const result = filesize(1, { precision: 50 });
473+
assert.ok(typeof result === 'string');
474+
});
475+
476+
it('should attempt to trigger the elusive p===1 branch in recalculation', () => {
477+
// Numbers that would produce scientific notation with precision=1
478+
// When e=0 (bytes), p=1, and toPrecision(1) might produce "1e+1" etc.
479+
const result = filesize(10, { precision: 1, round: 0 });
480+
assert.ok(typeof result === 'string');
481+
});
482+
455483
it('should handle extremely large numbers with precision adjustment', () => {
456484
const extremeNumber = Math.pow(1024, 15); // Much larger than supported exponent
457485
const result = filesize(extremeNumber, { precision: 3 });

0 commit comments

Comments
 (0)