Skip to content

fix: harden input validation and edge-case handling in filesize() - #344

Merged
avoidwork merged 2 commits into
masterfrom
fix/harden-filesize-edge-cases
Sep 15, 2026
Merged

avoidwork merged 2 commits into
masterfrom
fix/harden-filesize-edge-cases

Conversation

@avoidwork

Copy link
Copy Markdown
Owner

Summary

Fixes issue #343 — hardens filesize() input validation and edge-case handling across all 57 confirmed edge cases.

Changes

Input validation

  • BigInt overflow: Unified the BigInt and number input paths so overflowing BigInts throw TypeError instead of returning "Infinity YB".
  • Float exponent: Non-integer positive exponents (e.g. 1.5) are floored to a valid integer instead of producing "NaN undefined".
  • String exponent: String exponents (e.g. "1") are coerced to a number so the SI special case resolves correctly ("1 kB" not "1 KB").
  • Precision range: precision is validated to 1-100, throwing a clean TypeError instead of a raw RangeError.
  • Output option: Invalid output values throw TypeError instead of silently returning a string.

Formatting

  • Negative fullform singular: -1 now uses the singular unit name ("-1 byte" not "-1 bytes").
  • Sign preservation: Negative values that round to zero keep their sign consistently across precision and non-precision paths.
  • Scientific notation: Pathological values like Number.MAX_VALUE no longer leak e+284 notation.

Documentation

  • Documented the input coercion contract and option precedence rules in the JSDoc.

Testing

  • Added regression tests for all 57 confirmed edge cases.
  • 255 tests pass (was 200).
  • 100% line/branch/function coverage maintained.

Related

- Unify BigInt and number input paths so overflowing BigInts throw TypeError
- Coerce string exponent to number so SI special case resolves correctly
- Floor non-integer positive exponents to prevent NaN from table indexing
- Use Math.abs for singular/plural so -1 uses singular unit name
- Preserve sign when a negative value rounds to zero
- Validate precision range (1-100) and throw clean TypeError
- Validate output option and throw TypeError for invalid values
- Expand scientific notation to full decimal to prevent e+ notation leak
- Document input coercion contract and option precedence in JSDoc
- Add regression tests for all 57 confirmed edge cases
@avoidwork
avoidwork merged commit b9f2e62 into master Sep 15, 2026
2 checks passed
@avoidwork
avoidwork deleted the fix/harden-filesize-edge-cases branch September 15, 2026 01:18
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.

fix: harden input validation and edge-case handling in filesize()

1 participant