Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions docs/TECHNICAL_DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,13 @@ Dispatches the formatted result array into the requested output type. Handles `A
- `symbol`: `result[1]` — what appears in string/array output
- `unit`: `u` — the base unit before customization

#### `applyPrecisionHandling(value, precision, e, num, isDecimal, bits, ceil, roundingFunc, round, exponent)`
#### `applyPrecisionHandling(value, precision, e, num, isDecimal, bits, ceil, exponent)`

Handles significant digit formatting via `Number.prototype.toPrecision()`. Corrects scientific notation by recalculating with incremented exponent.
Handles significant digit formatting via `Number.prototype.toPrecision()`. Corrects scientific notation and unit overflow by recalculating with incremented exponent.

**Key behavior:**
- `toPrecision()` can produce strings like `"1e+21"` — if detected and `e < 8`, recalculates with `e + 1`
- `toPrecision()` can produce strings like `"1e+21"`, or reach the unit ceiling (`"1000"` kB, `"1024"` KiB) — if detected and `e < 8`, recalculates with `e + 1`
- The recalculated value is not rounded to `round` first, so `precision` keeps every significant digit
- Only auto-increments in auto-exponent mode

#### `applyNumberFormatting(value, locale, localeOptions, separator, pad, round)`
Expand Down
2 changes: 0 additions & 2 deletions src/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ export function filesize(
isDecimal,
bits,
ceil,
roundingFunc,
round,
exponent,
);
result[0] = precisionResult.value;
Expand Down
38 changes: 8 additions & 30 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,31 +260,18 @@ export function calculateBigIntValue(num, e, isDecimal, bits, ceil, autoExponent
}

/**
* Optimized precision handling with scientific notation correction
* Optimized precision handling with unit overflow correction
* @param {number} value - Current value
* @param {number} precision - Precision to apply
* @param {number} e - Current exponent
* @param {number} num - Original number
* @param {boolean} isDecimal - Whether using decimal base
* @param {boolean} bits - Whether calculating bits
* @param {number} ceil - Ceiling value
* @param {Function} roundingFunc - Rounding function
* @param {number} round - Round value
* @param {number} exponent - Forced exponent (-1 for auto)
* @returns {Object} Object with value and e properties
*/
export function applyPrecisionHandling(
value,
precision,
e,
num,
isDecimal,
bits,
ceil,
roundingFunc,
round,
exponent,
) {
export function applyPrecisionHandling(value, precision, e, num, isDecimal, bits, ceil, exponent) {
if (typeof value === "string") {
value = parseFloat(value);
}
Expand All @@ -304,23 +291,14 @@ export function applyPrecisionHandling(

const autoExponent = exponent === -1 || isNaN(exponent);

// Handle scientific notation by recalculating with incremented exponent
if (result.includes(E) && e < 8 && autoExponent) {
// Move to the next unit when the significant digits overflow into
// scientific notation ("1.00e+3") or reach the unit ceiling ("1000 kB",
// "1024 KiB"). The new value is taken straight from the input so
// `precision` sees every digit, as it does in the main path.
if ((result.includes(E) || Number(result) >= ceil) && e < 8 && autoExponent) {
e++;
const { result: valueResult } = calculateOptimizedValue(num, e, isDecimal, bits, ceil);
let p;
if (round > 0) {
p = Math.pow(10, round);
} else {
p = 1;
}
let computed;
if (p === 1) {
computed = roundingFunc(valueResult);
} else {
computed = roundingFunc(valueResult * p) / p;
}
result = computed.toPrecision(precision);
result = valueResult.toPrecision(precision);
}

return { value: result, e };
Expand Down
51 changes: 9 additions & 42 deletions tests/unit/filesize-helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,65 +145,32 @@ describe("Helper Functions", () => {

describe("applyPrecisionHandling", () => {
it("should apply precision without scientific notation", () => {
const result = applyPrecisionHandling(1.5, 2, 1, 1024, false, false, 1024, Math.round, 2, -1);
const result = applyPrecisionHandling(1.5, 2, 1, 1024, false, false, 1024, -1);
assert.deepStrictEqual(result, { value: "1.5", e: 1 });
});

it("should handle scientific notation with increment when exponent is -1", () => {
// Test with a large number that would produce scientific notation
const result = applyPrecisionHandling(
1000000000000,
1,
3,
1e15,
true,
false,
1000,
Math.round,
2,
-1,
);
const result = applyPrecisionHandling(1000000000000, 1, 3, 1e15, true, false, 1000, -1);
assert(typeof result.value === "string");
assert(typeof result.e === "number");
});

it("should handle scientific notation with increment when exponent is NaN", () => {
// Test that NaN also triggers auto-increment like -1
const result = applyPrecisionHandling(
1000000000000,
1,
3,
1e15,
true,
false,
1000,
Math.round,
2,
NaN,
);
const result = applyPrecisionHandling(1000000000000, 1, 3, 1e15, true, false, 1000, NaN);
assert(typeof result.value === "string");
assert(typeof result.e === "number");
});

it("should not increment when exponent is forced", () => {
// Test that forced exponent prevents auto-increment
const result = applyPrecisionHandling(
1000000000000,
1,
3,
1e15,
true,
false,
1000,
Math.round,
2,
5,
);
const result = applyPrecisionHandling(1000000000000, 1, 3, 1e15, true, false, 1000, 5);
assert.deepStrictEqual(result, { value: "1e+12", e: 3 });
});

it("should not increment when e >= 8", () => {
const result = applyPrecisionHandling(1.5, 2, 8, 1024, false, false, 1024, Math.round, 2, -1);
const result = applyPrecisionHandling(1.5, 2, 8, 1024, false, false, 1024, -1);
assert.deepStrictEqual(result, { value: "1.5", e: 8 });
});
});
Expand Down Expand Up @@ -335,7 +302,7 @@ describe("handleZeroValue with explicit symbol", () => {

describe("applyPrecisionHandling with string value", () => {
it("should parse string value and apply precision", () => {
const result = applyPrecisionHandling("1.5", 2, 1, 1024, false, false, 1024, Math.round, 2, -1);
const result = applyPrecisionHandling("1.5", 2, 1, 1024, false, false, 1024, -1);
assert.deepStrictEqual(result, { value: "1.5", e: 1 });
});

Expand Down Expand Up @@ -504,20 +471,20 @@ describe("formatOutput edge cases", () => {
describe("applyPrecisionHandling validation", () => {
it("should throw TypeError for out-of-range precision", () => {
assert.throws(
() => applyPrecisionHandling(1.5, 101, 1, 1024, false, false, 1024, Math.round, 2, -1),
() => applyPrecisionHandling(1.5, 101, 1, 1024, false, false, 1024, -1),
TypeError,
);
});

it("should throw TypeError for non-numeric precision", () => {
assert.throws(
() => applyPrecisionHandling(1.5, "foo", 1, 1024, false, false, 1024, Math.round, 2, -1),
() => applyPrecisionHandling(1.5, "foo", 1, 1024, false, false, 1024, -1),
TypeError,
);
});

it("should floor non-integer precision", () => {
const result = applyPrecisionHandling(1.5, 2.5, 1, 1024, false, false, 1024, Math.round, 2, -1);
const result = applyPrecisionHandling(1.5, 2.5, 1, 1024, false, false, 1024, -1);
assert.strictEqual(result.value, "1.5");
});
});
34 changes: 31 additions & 3 deletions tests/unit/filesize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ describe("filesize", () => {
it("should handle fullform with precision (string value path)", () => {
assert.strictEqual(
filesize(1058223158, { standard: "iec", precision: 3, fullform: true }),
"0.990 gibibytes",
"0.986 gibibytes",
);
assert.strictEqual(filesize(1234567890, { precision: 2, fullform: true }), "1.2 gigabytes");
});
Expand Down Expand Up @@ -350,6 +350,34 @@ describe("filesize", () => {
assert.strictEqual(filesize(0.5, { precision: 1 }), "1 B");
});

it("should move to the next unit when precision reaches the unit ceiling", () => {
// 999.999 kB to 4 significant digits is "1000"; it must not print as
// "1000 kB" when the same value without precision prints "1 MB".
assert.strictEqual(filesize(999999, { precision: 4 }), "1.000 MB");
assert.strictEqual(filesize(999999, { precision: 5 }), "1.0000 MB");
assert.strictEqual(filesize(-999999, { precision: 4 }), "-1.000 MB");
assert.strictEqual(filesize(1048575, { base: 2, precision: 4 }), "1.000 MiB");
assert.strictEqual(filesize(124999.99, { bits: true, precision: 4 }), "1.000 Mbit");
assert.deepStrictEqual(filesize(999999, { precision: 4, output: "object" }), {
value: "1.000",
symbol: "MB",
exponent: 2,
unit: "MB",
});
assert.strictEqual(filesize(999999, { precision: 4, output: "exponent" }), 2);
// a forced exponent keeps the unit
assert.strictEqual(filesize(999999, { precision: 4, exponent: 1 }), "1000 kB");
});

it("should keep every significant digit after moving to the next unit", () => {
// 1000 KiB is 0.9766 MiB; the value used to be rounded to `round`
// decimals (0.98) before toPrecision(3), printing 0.980.
assert.strictEqual(filesize(1024000, { base: 2, precision: 3 }), "0.977 MiB");
assert.strictEqual(filesize(1034240, { base: 2, precision: 3 }), "0.986 MiB");
assert.strictEqual(filesize(128000, { base: 2, bits: true, precision: 3 }), "0.977 Mibit");
assert.strictEqual(filesize(999500, { precision: 3 }), "1.00 MB");
});

it("should apply locale formatting when precision is set", () => {
// A string value from toPrecision ignores toLocaleString, so the
// locale's decimal separator was previously dropped (1.2, not 1,2).
Expand Down Expand Up @@ -588,10 +616,10 @@ describe("filesize", () => {

it("should handle precision with specific test case for GiB values", () => {
// Test specific precision case: filesize(1058223158, {base: 2, standard: 'iec', precision: 3})
// This should output '0.990 GiB'
// 1058223158 B = 1009.2 MiB = 0.9855 GiB, so three significant digits are 0.986
assert.strictEqual(
filesize(1058223158, { base: 2, standard: "iec", precision: 3 }),
"0.990 GiB",
"0.986 GiB",
);
});

Expand Down