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
90 changes: 0 additions & 90 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@ export function decorateResult(
bits,
roundingFunc,
) {
// Capture the number before the sign and the formatting: a comma decimal
// separator makes parseFloat read "1,5" as 1, and a leading "-" breaks the
// "=== 1" singular check.
let numericValue;
if (typeof result[0] === "string") {
numericValue = parseFloat(result[0]);
} else {
numericValue = result[0];
}

if (neg) {
// `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
Expand All @@ -413,16 +423,6 @@ export function decorateResult(
result[1] = symbols[result[1]];
}

// Capture the numeric value before formatting; a comma decimal separator
// (via separator or a locale such as de-DE) would otherwise make parseFloat
// read "1,5" as 1 and select the singular unit name.
let numericValue;
if (typeof result[0] === "string") {
numericValue = parseFloat(result[0]);
} else {
numericValue = result[0];
}

result[0] = applyNumberFormatting(
result[0],
locale,
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 @@ -268,6 +268,16 @@ describe("filesize", () => {
);
});

it("should keep the singular fullform name for negative one-magnitude values", () => {
assert.strictEqual(filesize(-1, { fullform: true }), "-1 byte");
assert.strictEqual(filesize(-0.125, { bits: true, fullform: true }), "-1 bit");
});

it("should keep the plural fullform name for negative values", () => {
assert.strictEqual(filesize(-2, { fullform: true }), "-2 bytes");
assert.strictEqual(filesize(-0.25, { bits: true, fullform: true }), "-2 bits");
});

it("should handle bits fullform singular", () => {
assert.strictEqual(filesize(0.125, { bits: true, fullform: true }), "1 bit");
});
Expand Down