Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ const STANDARD_CONFIGS = {
* @returns {Object} Configuration object
*/
export function getBaseConfiguration(standard, base) {
// Case-normalize: the JSDoc advertises "SI, IEC, JEDEC", but the lookup
// keys are lowercase; a wrong-case spelling of a valid standard must not
// silently fall back to a different unit system. The parameter is a string
// by contract (the caller destructures a "" default), so a non-string is
// invalid input and fails fast right here. Unknown values keep the
// existing fallback behavior.
standard = standard.toLowerCase();

// Use cached lookup table for better performance
if (STANDARD_CONFIGS[standard]) {
return STANDARD_CONFIGS[standard];
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/filesize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1474,3 +1474,28 @@ describe("partial structuredClone", () => {
assert.strictEqual(format2(1000), "1 second");
});
});

describe("standard option case-insensitivity", () => {
it("should accept uppercase IEC", () => {
assert.strictEqual(filesize(1024, { standard: "IEC" }), "1 KiB");
assert.strictEqual(filesize(1048576, { standard: "IEC" }), "1 MiB");
});

it("should accept uppercase SI", () => {
assert.strictEqual(filesize(1000, { standard: "SI" }), "1 kB");
});

it("should accept uppercase JEDEC", () => {
assert.strictEqual(filesize(1024, { standard: "JEDEC" }), "1 KB");
assert.strictEqual(filesize(1048576, { standard: "JEDEC" }), "1 MB");
});

it("should keep the fallback for unknown standards", () => {
assert.strictEqual(filesize(1024, { standard: "foobar" }), "1.02 kB");
});

it("should accept uppercase standard in partial()", () => {
const format = partial({ standard: "IEC" });
assert.strictEqual(format(1048576), "1 MiB");
});
});
Loading