fix: support scientific notation in parse() for roundtrip consistency#285
Open
mahmoodhamdi wants to merge 1 commit intovercel:mainfrom
Open
fix: support scientific notation in parse() for roundtrip consistency#285mahmoodhamdi wants to merge 1 commit intovercel:mainfrom
mahmoodhamdi wants to merge 1 commit intovercel:mainfrom
Conversation
format() produces scientific notation for very large millisecond values (e.g., `ms(Number.MAX_SAFE_INTEGER)` → `"285616y"`), and for even larger values JavaScript's number-to-string coercion uses exponential notation (e.g., `"5.7e+297y"`). The parse() regex did not accept the `e` notation, causing it to return NaN for format()'s own output. Update the regex number pattern from `-?\d*\.?\d+` to `-?\d*\.?\d+(?:e[+-]?\d+)?` so that parseFloat-compatible scientific notation is matched correctly. Fixes vercel#284
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
format()can produce scientific notation for very large millisecond values (e.g., JavaScript coerces large numbers to strings like"5.7e+297y"), butparse()returnsNaNfor these strings because its regex doesn't match theenotation. This breaks the roundtrip contract between the two functions.I ran into this while using ms for cache TTL calculations where intermediate values can get large before being clamped —
parse(format(bigValue))silently returningNaNwas unexpected.Changes
parse()from-?\d*\.?\d+to-?\d*\.?\d+(?:e[+-]?\d+)?to accept scientific notation1e3ms,1.5e2ms,1e+3ms,1e-3ms)parse(format(N))does not returnNaNfor large valuesms()Checks
pnpm test— 173 tests pass (Node.js + Edge Runtime)pnpm typecheck— cleanpnpm lint— cleanFixes #284