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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fwiw, checking
isNaN(value)
should also work here sinceDate.prototype.valueOf()
will returnNaN
when the date is invalid.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that’s changed to
isNaN(value)
, I’d like to see a comment explaining more or less exactly what @jasnell just wrote. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:-) not saying that it needs to be changed, only that it should work ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Number.isNaN(value.valueOf())
seems like the better way to go to me, even if it does require a code comment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather see
value.getTime()
and compare that to an invalid date'sgetTime()
that way it's obvious and no comment is required.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Insofar that it matters, the way V8 implements the Date object, getTime() is faster than valueOf() is faster than toString(). The first one is a field lookup, the others are calls into the runtime, with toString() polluting the date cache and creating a new string every time.
I'd write it as
Number.isNaN(value.getTime())
orObject.is(value.getTime(), NaN)
to avoid keeping around an extra object, though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me.
On Apr 30, 2016 3:05 AM, "Ben Noordhuis" notifications@github.com wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bnoordhuis well, it means keeping an extra date object once. It can be kept in a utility and used everywhere invalid dates are needed :P
I'm fine with
Number.isNaN(value.getTime(), NaN)
although it's not obvious to readers who are not familiar with this obscure detail.