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
5 changes: 4 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ export function decorateResult(
roundingFunc,
) {
if (neg) {
result[0] = -result[0];
// `precision` leaves the value as a string from toPrecision (e.g. "1.50").
// Negating that arithmetically coerces it back to a number and drops the
// trailing zeros the option asked for, so prefix the sign instead.
result[0] = typeof result[0] === "string" ? `-${result[0]}` : -result[0];
}

if (symbols[result[1]]) {
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/filesize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,16 @@ describe("filesize", () => {
assert.strictEqual(filesize(-1234567890, { precision: 2 }), "-1.2 GB");
});

it("should keep precision trailing zeros for negative values", () => {
// A negative value must carry the same significant digits as its positive counterpart
assert.strictEqual(filesize(1500, { precision: 3 }), "1.50 kB");
assert.strictEqual(filesize(-1500, { precision: 3 }), "-1.50 kB");
assert.strictEqual(filesize(-1000, { precision: 3 }), "-1.00 kB");
assert.strictEqual(filesize(-1, { precision: 3 }), "-1.00 B");
assert.deepStrictEqual(filesize(-1500, { precision: 3, output: "array" }), ["-1.50", "kB"]);
assert.strictEqual(filesize(-1500, { precision: 3, output: "object" }).value, "-1.50");
});

it("should ensure no scientific notation in any precision result", () => {
// Test a range of numbers that would normally produce scientific notation from toPrecision
// but should have it removed by our implementation
Expand Down
Loading