Skip to content

fix: validate precision and output for zero values - #436

Open
samipuro wants to merge 1 commit into
avoidwork:masterfrom
samipuro:fix/zero-option-validation
Open

samipuro wants to merge 1 commit into
avoidwork:masterfrom
samipuro:fix/zero-option-validation

Conversation

@samipuro

@samipuro samipuro commented Dec 2, 2026

Copy link
Copy Markdown

Summary

filesize(0, ...) returns early through handleZeroValue(), which skips the precision and output checks added in #344. So filesize(0, { precision: 200 }) throws a raw RangeError from toPrecision() instead of TypeError: Invalid precision, and filesize(0, { output: "foo" }) returns "0 B" instead of throwing TypeError: Invalid output: foo as filesize(1, { output: "foo" }) does. handleZeroValue() now applies the same checks before formatting. Valid inputs are unchanged.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature / enhancement
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Build / dependency / tooling change
  • Test additions or updates
  • Other

Related Issues

Refs #343

Testing

Added a regression test for zero with an out-of-range, fractional and string precision, and with an invalid output (number and BigInt). It fails on master and passes with the fix. Unit tests pass with 100% coverage (except one locale test that also fails on master on my machine because of its decimal comma locale), and oxlint and oxfmt are clean on src and tests. I did not run the build (npm test checks the formatting of the built files, so I left both boxes to CI), and dist/ is unchanged.

Checklist

  • npm test passes
  • npm run build succeeds
  • 100% test coverage maintained
  • No hardcoded secrets or credentials introduced
  • Zero external dependencies added
  • ES Modules only (no CommonJS in src/)
  • JSDoc comments added/updated
  • CHANGELOG.md updated (if applicable)

This branch has not been deployed

No deployments
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.

1 participant