From c1ff9549152be0f9e111790e921858ae7d5aba4c Mon Sep 17 00:00:00 2001 From: Rahul Ranjan Date: Mon, 9 Nov 2026 11:29:16 +0000 Subject: [PATCH 1/2] fix: accept uppercase spellings of the standard option The JSDoc for the `standard` option advertises "SI, IEC, JEDEC", but STANDARD_CONFIGS is keyed on lowercase "si"/"iec"/"jedec", so any uppercase spelling missed the lookup and silently fell through to the decimal default: filesize(1048576, { standard: "iec" }) // "1 MiB" filesize(1048576, { standard: "IEC" }) // "1.05 MB" (wrong unit system) Case-normalize the value in getBaseConfiguration() before the lookup. Unknown values keep the existing fallback behavior (pinned by the "should fall back to JEDEC default for unknown standard" test). Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- src/helpers.js | 6 ++++++ tests/unit/filesize.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/helpers.js b/src/helpers.js index fad6208..eb57009 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -40,6 +40,12 @@ 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. Unknown values keep the + // existing fallback behavior. + standard = typeof standard === "string" ? standard.toLowerCase() : standard; + // 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"); + }); +}); From 13a8749fbfed7e4633203acfcf1f0b7f4262bddc Mon Sep 17 00:00:00 2001 From: Rahul Ranjan Date: Thu, 12 Nov 2026 12:16:42 +0000 Subject: [PATCH 2/2] Drop the typeof guard on standard normalization The standard parameter is a string by contract (the filesize entry point destructures a "" default), so a non-string is invalid input: the lookup ops right after would fail on it anyway. Normalize unconditionally and let bad input fail fast at the toLowerCase call. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- src/helpers.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index eb57009..0b4c177 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -42,9 +42,11 @@ const STANDARD_CONFIGS = { 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. Unknown values keep the + // 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 = typeof standard === "string" ? standard.toLowerCase() : standard; + standard = standard.toLowerCase(); // Use cached lookup table for better performance if (STANDARD_CONFIGS[standard]) {