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

Correct bad Date.prototype.toJSON polyfills from elsewhere #95

Closed
petermichaux opened this issue Jan 29, 2012 · 7 comments · Fixed by #98
Closed

Correct bad Date.prototype.toJSON polyfills from elsewhere #95

petermichaux opened this issue Jan 29, 2012 · 7 comments · Fixed by #98

Comments

@petermichaux
Copy link
Contributor

Douglas Crockford's json2.js, for example, defines a Date.prototype.toJSON function that does not pad extended years with zeros as the es5-shim does in Date.prototype.toISOString does.

es5-shim can redefine a bad Date.prototype.toJSON by changing

if (!Date.prototype.toJSON) {

to

if (!Date.prototype.toJSON ||
    (new Date(-62198755200000).toJSON().indexOf('-000001') === -1)) {

This also means people using json2.js need to include json2.js in their environment before including es5-shim as defining Date.prototype.toJSON means other things will not be defined in json2.js

@petermichaux
Copy link
Contributor Author

json2.js Date.prototoype.toJSON is also not generic. If json2.js is updated to handle extended dates correctly but not to be generic then the following check would fix things.

if (!Date.prototype.toJSON ||
    // does Date.prototype.toJSON not do extended years properly?
    ((new Date(-62198755200000)).toJSON().indexOf('-000001') === -1) ||
    // is Date.prototype.toJSON non-generic?
    (function() {
        var str = 'abc';
        try {
            return Date.prototype.toJSON.call({toISOString:function(){return str;}}) !== str;
        }
        catch (err) {
            return true;
        }
    }())) {

@kriskowal
Copy link
Member

Thanks, @petermichaux. Good to cross paths again!

@ghost ghost assigned bryanforbes Jan 29, 2012
@petermichaux
Copy link
Contributor Author

I admit the above feature test is ugly.

A workaround that could be documented in the README?...

<script src="json2.js" type="text/javascript"></script>
<script type="text/javascript">
    delete Date.prototype.toJSON
</script>
<script src="es5-shim.js" type="text/javascript"></script>

@petermichaux
Copy link
Contributor Author

By the way, Crockford closed my tickets about improving the Date.prototype.toJSON in his json2.js.

@bryanforbes
Copy link
Contributor

What about something like this?

if (!Date.prototype.toJSON ||
    // does Date.prototype.toJSON not do extended years properly?
    !~((new Date(-62198755200000)).toJSON().indexOf('-000001')) ||
    // is Date.prototype.toJSON non-generic?
    ~(function() {
        try {
            return Date.prototype.toJSON.call({toISOString:function(){return -1;}});
        } catch (err) {
            return true;
        }
    }())) {

@jdalton
Copy link
Contributor

jdalton commented Jan 30, 2012

@jdalton
Copy link
Contributor

jdalton commented Jan 30, 2012

Additional json2.js issues:
douglascrockford/JSON-js#28

@Yaffle Yaffle closed this as completed May 11, 2013
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 a pull request may close this issue.

5 participants