diff --git a/src/helpers.js b/src/helpers.js index fad6208..0b4c177 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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]; diff --git a/tests/unit/filesize.test.js b/tests/unit/filesize.test.js index 7295d4d..69a9495 100644 --- a/tests/unit/filesize.test.js +++ b/tests/unit/filesize.test.js @@ -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"); + }); +});