Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dates #16

Closed
wants to merge 1 commit into from
Closed

Support dates #16

wants to merge 1 commit into from

Conversation

MoOx
Copy link

@MoOx MoOx commented Apr 29, 2016

Closes #15

There is a little trick to test if a value is a date because Date.prototype.toJSON is used before we can access the value in the stringify replacement function, so we end up with a date.toISOString() result.

@yahoocla
Copy link

Thank you for submitting this pull request, however I do not see a valid CLA on file for you. Before we can merge this request please visit https://yahoocla.herokuapp.com/ and agree to the terms. Thanks! 😄

@MoOx
Copy link
Author

MoOx commented Apr 29, 2016

CLA signed.

@MoOx
Copy link
Author

MoOx commented Apr 29, 2016

One caveat with: string that will match the ISO date format will be converted as Date, which might be an unexpected behavior (or just a breaking change). But I don't know how we can change this.
On other trick might be to change Date.prototype.toJSON (temporarly, before JSON.stringify, then revert it) to have more control on the output. That will allow us to avoid the caveat explain above.

var DATE_LENGTH = new Date().toISOString().length
function isDate(d) {
try {
return (
Copy link
Contributor

Choose a reason for hiding this comment

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

the try/catch might bring some slowness here, not sure.

Copy link
Author

Choose a reason for hiding this comment

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

If we don't use the try/catch we must validate the date. It seems that d.getTime() will not throw, so we could test using isNaN().

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can use isFinite() to see if it's a valid date without this try/catch.

Copy link
Author

Choose a reason for hiding this comment

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

Will try that.

@yahoocla
Copy link

yahoocla commented May 5, 2016

CLA is valid!

@@ -25,13 +25,31 @@ var UNICODE_CHARS = {
'\u2029': '\\u2029'
};

// We can‘t just instanceof Date since dates are already converted to strings
// because of native Date.prototype.JSON (which use toISOString)
var DATE_LENGTH = new Date().toISOString().length
Copy link
Collaborator

Choose a reason for hiding this comment

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

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 characters long: YYYY-MM-DDTHH:mm:ss.sssZ.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

Can you just set this to 24?

Copy link
Author

Choose a reason for hiding this comment

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

Indeed, but this is more clear the way I coded it (imo) and it's computed only once per runtime, so no big deal (and clearer) imo.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I disagree. I think setting it to 24 with a comment that it'll always be 24 is a better approach. But this is likely moot given my two comments above: #16 (comment)

@ericf ericf mentioned this pull request May 5, 2016
@ericf
Copy link
Collaborator

ericf commented May 25, 2016

@MoOx fyi, I added a PR to create a hot-path that doesn't use the JSON.stringify() replacer function: #17. Given the dates are more involved, please comment in that PR to let me know what you think and maybe a better options name than isJSON.

@gchudnov
Copy link

Hello guys,
any plans to merge this PR?

@ericf
Copy link
Collaborator

ericf commented Jul 14, 2016

@gchudnov The conversation here provides the current state of this PR, i.e. it's not currently merge-able.

@ethanresnick ethanresnick mentioned this pull request Mar 26, 2017
@okuryu
Copy link
Collaborator

okuryu commented Jul 15, 2017

#27 has been merged.

@okuryu okuryu closed this Jul 15, 2017
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.

6 participants