Skip to content

fix: accept uppercase spellings of the standard option - #403

Merged
avoidwork merged 2 commits into
avoidwork:masterfrom
rahul05ranjan:fix/standard-option-case
Nov 13, 2026
Merged

avoidwork merged 2 commits into
avoidwork:masterfrom
rahul05ranjan:fix/standard-option-case

Conversation

@rahul05ranjan

Copy link
Copy Markdown
Contributor

What

The JSDoc for the standard option advertises "SI, IEC, JEDEC", but the STANDARD_CONFIGS lookup is keyed on the lowercase "si" / "iec" / "jedec", so an uppercase spelling misses the lookup and silently falls through to the decimal default — the wrong unit system:

filesize(1048576, { standard: "iec" }); // "1 MiB"
filesize(1048576, { standard: "IEC" }); // "1.05 MB"  <- decimal fallback, no warning
filesize(1000, { standard: "SI" });     // "1.00 kB"  <- happens to look right, by accident

Fix

Case-normalize the value in getBaseConfiguration() before the cached lookup. Unknown values keep the existing fallback behavior, which is pinned by the should fall back to JEDEC default for unknown standard test, so this change only affects spellings that are case variants of the three real standards — exactly the ones the JSDoc shows. The same normalization covers partial({ standard: "IEC" }).

Verification

  • New tests in tests/unit/filesize.test.js (uppercase IEC/JEDEC/SI, the partial() path, and a guard that the unknown-value fallback is unchanged): 3 fail on main@d2bb4c1, all pass with this change.
  • Full suite with the fix: node --test tests/unit/ — 278 pass, 0 fail (273 pre-existing + 5 new).

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 <[email protected]>
Comment thread src/helpers.js Resolved
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 <[email protected]>
@rahul05ranjan

Copy link
Copy Markdown
Contributor Author

Fair point — guard dropped in 13a8749. The parameter is a string by contract (the entry point destructures a """ default, so undefined never reaches this function), which makes the typeof check dead weight: a non-string is invalid input and the lookup ops would fail on it anyway. Normalization is now an unconditional standard.toLowerCase(), so bad input fails fast right at that call, consistent with how the option validations in the entry point behave. Unknown string values keep the existing fallback.

@avoidwork

Copy link
Copy Markdown
Owner

Why is this a problem? JEDEC is pronounced as 'jedec' in conversation like 'laser'

@avoidwork

avoidwork commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

I'm thinking about my day to day work, where I often use acronyms and my peers also lowercase in slack, email, code, etc., e.g. i often see 'bcp47' or 'bcp-47' but rarely (almost never) do I see BCP-47. I'm targeting the audience with the original version (lower case).

@rahul05ranjan

Copy link
Copy Markdown
Contributor Author

Thanks for the context — that makes sense: lowercase is the spelling people actually type day to day, and this change does not touch the lowercase path at all.

The one problem it fixes is silent misbehavior with the spelling the API itself documents: the JSDoc advertises "SI, IEC, JEDEC", but today passing standard: "IEC" (mixed case, exactly as documented) skips the lookup and silently falls back to the decimal default — a wrong unit system with no warning. The PR is purely additive: it case-normalizes the lookup key so the documented spellings resolve to the configs they name, lowercase and unknown-value behavior stay exactly as before, and the tests pin the lowercase/fallback paths we checked.

Happy to keep it as-is, or if you would rather document lowercase-only and reject other spellings explicitly, I can rework it that way instead. Your call — just let me know.

@avoidwork

Copy link
Copy Markdown
Owner

great point about the docs - that'd always repeat over time. lets merge this.

@avoidwork
avoidwork merged commit 4bd3c30 into avoidwork:master Nov 13, 2026
2 checks passed
@avoidwork avoidwork mentioned this pull request Nov 13, 2026
9 of 15 tasks
@avoidwork

Copy link
Copy Markdown
Owner

This is 11.0.32

@rahul05ranjan

Copy link
Copy Markdown
Contributor Author

Thank you for the review and the merge! Glad the docs reasoning landed — and thanks for shipping it in 11.0.32. Happy to contribute again anytime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants