Skip to content

fix(parse): tolerate leading/trailing whitespace - #304

Open
Ultroncn wants to merge 1 commit into
vercel:mainfrom
Ultroncn:fix/parse-trim-whitespace
Open

Ultroncn wants to merge 1 commit into
vercel:mainfrom
Ultroncn:fix/parse-trim-whitespace

Conversation

@Ultroncn

Copy link
Copy Markdown

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 returns NaN: parse(' 1s') → NaN, parse('1s ') → NaN. Values copied from config files or environment variables often carry such whitespace, and the silent NaN can 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 returns NaN instead of throwing. parseStrict() goes through parse(), so it is covered too.

Tests: added cases in src/parse.test.ts and src/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 --runInBand 169/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.

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