Skip to content

Fix toJSON casting of invalid moment - #2887

Closed
wraithgar wants to merge 2 commits into
moment:developfrom
wraithgar:invalid_null_date
Closed

wraithgar wants to merge 2 commits into
moment:developfrom
wraithgar:invalid_null_date

Conversation

@wraithgar

Copy link
Copy Markdown
Contributor

This is an attempt at solving issue #2886.

Invalid moments were casting as 'null' (the string)
Now they are properly casting as null (the literal)

Invalid moments were casting as 'null' (the string)
Now they are properly casting as null (the literal)
Comment thread src/lib/moment/to-type.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should just show new Date(NaN).toJSON() === null. It's the JSON.stringify part that is confusing the issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks for the reminder, didn't think that one through enough. Fixed in commit dafaba3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@mattjohnsonpint mattjohnsonpint added this to the 2.12.0 milestone Jan 12, 2016
@mattjohnsonpint

Copy link
Copy Markdown
Contributor

LGTM. Staging for 2.12.0 (unless @ichernev decides to do a 2.11.2).

Thanks for your contribution!

@mattjohnsonpint

Copy link
Copy Markdown
Contributor

(seems silly to label a bug fix with "bug") :)

@icambron

Copy link
Copy Markdown
Contributor

Do we know why it was made a string in the first place? Seems like a really explicit decision in d4a1694

@mattjohnsonpint

Copy link
Copy Markdown
Contributor

@icambron - The comment leads me to believe there was a misunderstanding between toJSON and JSON.stringify. Consider:

JSON.stringify(NaN)    // "null"
JSON.stringify(null)   // "null"
JSON.stringify('null') // ""null""

JSON.stringify(new Date(NaN)) // "null"
new Date(NaN).toJSON() // null

Our implementation should match the Date object's toJSON implementation

@icambron

Copy link
Copy Markdown
Contributor

👍

@ichernev

ichernev commented Mar 6, 2016

Copy link
Copy Markdown
Contributor

Merged in 2255756

@ichernev ichernev closed this Mar 6, 2016
ichernev added a commit that referenced this pull request Mar 6, 2016
Fix toJSON casting of invalid moment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants