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

Interoperable $.contains method #1

Open
ryanve opened this issue Apr 15, 2013 · 0 comments
Open

Interoperable $.contains method #1

ryanve opened this issue Apr 15, 2013 · 0 comments

Comments

@ryanve
Copy link
Owner

ryanve commented Apr 15, 2013

To improve interoperability, it seems wise to alias scan.contains as something else. Currently scan aliases scan.contains as scan.inNode. Maybe scan.descends is a better name?

Compare jQuery.contains vs. _.contains vs "".contains. Something like this combines those:

function contains(ob, needle, i) {
    if (ob.nodeType)
        return inNode(ob, needle);
    i >>= 0;
    if (ob.indexOf)
        return !!~ob.indexOf(needle, i);
    var l = ob.length;
    for (i = 0 > i ? i + l : i; i < l; i++) {
        if (i in ob && ob[i] === needle)
            return true;
    } return false;
}

Yet it seems repetitive to recreate .indexOf functionality here. It is unneeded internally and could be provided (to an ender build, etc.) as needed by a utility module such as underscore, who aliases _.contains as _.include:

function contains(ob, needle, i) {
    return ob.nodeType ? inNode(ob, needle) : this.include(ob, needle, i);
}

_.include also supports checking in non-array-like objects.

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

No branches or pull requests

1 participant