Skip to content

Gracefully handle Date.parse receiving a number #261

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

Closed
wants to merge 1 commit into from

Conversation

jimmed
Copy link

@jimmed jimmed commented Dec 21, 2016

I'm using a Vis.js Timeline component, which internally calls Date.parse with a number when doing some type coersion.

Calling the native parse function with a number should either return another number, or NaN (although this isn't covered by the spec, IE/FF/Chrome all behave as such). However, because datejs's parser doesn't check for a number input, it falls through to attempting to normalise it as a string, which fails when hitting parseUtils.removeOrds.

Naturally, removing datejs and thus reverting to the native Date.parse method fixes this issue, but this is not an option for me as datejs's functionality is used throughout my application.

Please review this pull request, as I'm unsure whether handling numbers is something you want this method to do.

I'm using a Vis.js Timeline component, which [internally calls `Date.parse` with a number](https://github.com/almende/vis/blob/master/lib/util.js#L395) when doing some type coersion.

Calling the native parse function with a number should either return another number, or `NaN`. However, because datejs's parser doesn't check for a number input, it falls through to attempting to normalise it as a string, which fails when hitting `parseUtils.removeOrds`.

Naturally, removing datejs and thus reverting to the native `Date.parse` method fixes this issue.

I am happy to raise a PR with a fix, but I'm unsure whether handling numbers is something you want this method to do.
@abritinthebay
Copy link
Owner

This would break a crap-ton of formats like 20120630 and things like that.

Vis.js is the one that is incorrect here anyhow - Date.parse() is, per spec, a string parsing function.

They're using it wrong. Open a PR with them - this is a won't fix. It breaks things, isn't a bug with this library, and is to work around people abusing non-spec behavior.

@jimmed
Copy link
Author

jimmed commented Dec 22, 2016

Thanks, I appreciate the quick response!

I was pretty sure this would be the case, but I figured it was worth double-checking. I'll raise a fix with Vis.js and link back here for posterity.

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.

2 participants