Repository navigation
Conversation
parse() already accepts spaces between the number and the unit
('1 s'), but a leading or trailing space (' 1s', '1s ') returned
NaN — a common surprise for values copied from config files or
environment variables, where the result silently becomes NaN and can
turn a timeout into an immediate one.
Match against the trimmed input. The length and empty-string checks
still use the original string, so whitespace-only input still
returns NaN instead of throwing.
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.
Hi, thanks for ms. Small inconsistency fix in
parse().Bug:
parse()already accepts spaces between the number and the unit ('1 s'→ 1000), but a leading or trailing space returnsNaN:parse(' 1s')→NaN,parse('1s ')→NaN. Values copied from config files or environment variables often carry such whitespace, and the silentNaNcan turn a timeout into an immediate one.Fix: match against
str.trim()instead of the raw string. The length and empty-string checks still use the original string, so whitespace-only input still returnsNaNinstead of throwing.parseStrict()goes throughparse(), so it is covered too.Tests: added cases in
src/parse.test.tsandsrc/parse-strict.test.ts(leading/trailing spaces, tabs/newlines, whitespace-only →NaN). New tests fail on the old code and pass with the fix. Full suite:npx jest --runInBand169/169 passing.Note on #280: I saw #280 proposed the same trim, but it was closed by its author without maintainer review, so I could not tell whether it was ever evaluated. This is a fresh, minimal take (3-line change + 2 tests, no new files) — happy to close it if you would rather not take this on.