Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Use the has API #19

Closed
jeffrose opened this issue Oct 5, 2012 · 4 comments
Closed

Use the has API #19

jeffrose opened this issue Oct 5, 2012 · 4 comments

Comments

@jeffrose
Copy link

jeffrose commented Oct 5, 2012

Use the has API instead of stringifySupported and parseSupported in order to take advantage of has-aware AMD optimizers, like r.js and dojo.

function has( id ){
    if( id === 'json' ){
        return !!JSON;
    } else if( id === 'json-stringify' ){
        return typeof JSON.stringify === "function";
    } else if( id === 'json-parse' ){
        return typeof JSON.parse === "function";
    } else {
        return false;
    }
}
if( has( 'json-stringify' ) ){
...
}
if( has( 'json-parse' ) ){
...
}
@ghost
Copy link

ghost commented Oct 8, 2012

Thanks for the report!

The problem with the suggested implementation is that it's not strictly equivalent to stringifySupported and parseSupported. has tests for existence, whereas JSON 3 performs feature tests to determine if the (possibly native) implementation is incorrect or incomplete. These bugs aren't restricted to older environments; browsers as recent as Safari 5.1 and IE 10 fail the tests. The tests are also important for detecting buggy libraries and polyfills (e.g., Prototype <= 1.6.1, JSON 2).

I can contribute JSON 3's feature tests to the has.js project (I presume that's what has-aware optimizers like Dojo use?)...but then devs will need to upgrade to the latest version of has.js for consistent behavior. If possible, I'd like to avoid tying JSON 3 to a particular version of another project.

I'm not familiar with how Dojo's optimizer works, so please forgive me if I'm misunderstanding the thrust of the issue.

@jeffrose
Copy link
Author

jeffrose commented Oct 8, 2012

Actually while has.js and dojo introduced the has API, from an optimizer perspective any library can implement it. For example poly.js uses it and Modernizr has an issue open to make use of it as well.

It is possible to configure the r.js and dojo optimizers to build for browsers with particular features. So instead of having a run-time decision, you can make the decision at build-time. For example...

// build.js
( {
    baseUrl: '.',
    ...
    has: {
        // Build for browsers that have valid JSON implementations
        'json-parse': true,
        'json-stringify': true,
        'json': true
    }
} )

... So code written like this...

...
if( has( 'json-parse' ) ){
    // Use native JSON.parse
} else {
    // Use polyfill
}
...

... becomes...

...
    // Use native JSON.parse
...

@ghost ghost closed this as completed in c54a4d7 Oct 9, 2012
@ghost
Copy link

ghost commented Oct 9, 2012

Ah, I see. Thanks very much for clarifying!

Does c54a4d7 look solid to you?

@jeffrose
Copy link
Author

jeffrose commented Oct 9, 2012

Looks good to me.

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

1 participant