fix: return NaN, not 0, for an unparseable quantity ("an hour" === 0) - #49
Open
MildlyMeticulous wants to merge 1 commit into
Open
Conversation
numbered.parse() returns 0 for any words it cannot parse, which is
indistinguishable from a genuine zero, so "an hour" and "a few minutes"
both came back as 0. For a scheduler an interval of 0 means "fire again
immediately", so this failed open into a hot loop rather than an error.
Treat "a"/"an" as 1, consistent with the bare-unit default and with the
already-supported "one hour", and return NaN for a quantity that is not
recognised, consistent with the existing humanInterval("foobar") === NaN
contract.
|
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.
The bug
humanInterval('an hour')returns0.Not
NaN, not3600000— zero. Same for'a day','a few minutes','some seconds','half an hour','couple of hours'.That last line is the one that makes this a bug rather than a missing feature: the suite already asserts
so unrecognised input is supposed to be
NaN.'a few minutes'is just as unrecognised, but it silently reports zero instead.Why it matters
The README points at Agenda and Bree, and both treat the return value as an interval. Zero is not an inert "nothing happened" value there — it means fire again immediately. So a typo'd or slightly-too-natural interval string doesn't throw, doesn't warn, and doesn't get skipped; it turns into a hot loop.
NaNfails loudly, which is what you want.Cause
numbered.parse()returns0for anything it can't parse, with no way to distinguish that from a real zero:humanIntervaltakes that0at face value and multiplies it by the unit.Fix
Two changes, both following behaviour the library already has:
a/an→ 1.'one hour'already works and a bare'hour'already defaults to1, so'an hour'agreeing with them is the consistent outcome.NaN. Matches the existing'foobar'contract. An explicit'zero seconds'still returns0.Verification
All 59 existing tests pass unchanged. 6 tests added (65 total); against the unpatched
index.js5 of them fail.Note on compatibility
This is a behaviour change for input that previously returned
0. I'd argue every one of those cases was already wrong — and dangerously so given the scheduler use case — but if you'd rather have'a'/'an'support without theNaNhalf, say the word and I'll split the commit.