honour Date object passed as legacy 2nd arg to isAfter / isBefore#2783
Open
HrachShah wants to merge 1 commit into
Conversation
…and isBefore
isAfter and isBefore support a legacy two-argument form `isAfter(str, date)` where the
second argument is a date, plus a modern one-argument form that takes an options
object with `comparisonDate`. The legacy/object disambiguation was done with
`typeof options === 'object'`, which is true for any Date object. So when a caller
wrote `isAfter('2010-01-02', new Date('2010-01-01'))` the legacy branch fired, the
code looked up `options.comparisonDate` on the Date (which is `undefined`), fell
back to `Date().toString()` for "now", and silently compared against the current
moment instead of the supplied Date. `isAfter('2010-01-02', new Date('2010-01-01'))`
returned `false` even though 2010-01-02 is after 2010-01-01, and `isBefore` had
the symmetric problem.
Hoist the disambiguation into a small `resolveComparisonDate` helper that
recognises a `Date` instance directly, and short-circuit `toDate()` when the
comparison date is already a `Date`. The `typeof === 'object'` branch is also
guarded against `null` for the same reason the rest of the codebase already
guards null options.
tests/testFunctions.js serialises args via `JSON.stringify` for error messages,
so I exercised the new path with a hand-built assertion rather than through the
shared `test` helper, which round-trips args through JSON and would lose the
Date identity. The new cases fail on master and pass on the fix.
- test/validators/isAfter.test.js: add a '(legacy syntax with Date object)' describe
block that asserts `isAfter('2010-01-02', new Date('2010-01-01'))` is true,
`isAfter('2009-12-31', new Date('2010-01-01'))` is false, and that an invalid
Date falls through to `false`.
- test/validators/isBefore.test.js: mirror the same three cases for isBefore.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2783 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2586 2599 +13
Branches 656 660 +4
=========================================
+ Hits 2586 2599 +13 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
What this PR fixes
isAfterandisBeforesilently ignore aDateobject that callers pass asthe legacy second argument. The disambiguation between "this is the legacy date
arg" and "this is the new options object" was a
typeof options === 'object'check, which is true for any
Dateas well, so the new-options branch alwayswon.
isAfter('2010-01-02', new Date('2010-01-01'))returnedfalseeventhough 2010-01-02 is plainly after 2010-01-01, because the code then did
options.comparisonDate(which isundefinedon a Date), fell back toDate().toString()for "now", and compared against the current moment.Repro
Fix
Hoist the disambiguation into a small
resolveComparisonDatehelper thatrecognises a
Dateinstance directly, and short-circuittoDate()when thecomparison date is already a
Date. Thetypeof === 'object'branch is alsoguarded against
nullfor the same reason the rest of the codebase alreadyguards null options.
Tests
The shared
testhelper intest/testFunctions.jsserialises args viaJSON.stringifyfor error messages, which would strip the Date identity. Thenew cases are written as direct assertions against the validator instead.
test/validators/isAfter.test.js— new '(legacy syntax with Date object)'describe block:
isAfter('2010-01-02', new Date('2010-01-01'))is trueisAfter('2009-12-31', new Date('2010-01-01'))is falseisAfter('2010-01-02', new Date('not a date'))is falsetest/validators/isBefore.test.js— same three cases forisBefore.Both new test blocks fail on master (
Stashed the source changes and ran the suite: 2 failing) and pass on the fix (320 passing).ESLint passes on the modified files.