Code Monkey home page Code Monkey logo

scan's Introduction

scan

standalone querySelectorAll selector engine with jQuery-like interface

$ npm install scan --save

Basic usage

var scan = require('scan')
scan('.example').find('a').not('[href^="#"]')

API (0.9)

  • scan() instances are array-like and inherit from scan.prototype and Array.prototype
  • scan() methods chain intuitively or are callable via scan.prototype[method].call(array)
  • Methods are generally compatible with jQuery methods of the same name

scan(query, context?)

  • scan(selector) → elements that match selector
  • scan(selector, node|nodes) → elements that match selector from node or any nodes
  • scan(node|nodes) → nodes wrapped in scan instance

.find(needle)

  • scan(query).find(selector) → descendants that match selector
  • scan(query).find(element|elements) → elements that descend from query
  • scan(stack).find(fn, scope?) → the first value to pass fn.call(scope, value, i, stack)

.filter(needle)

  • scan(query).filter(nodes, selector) → stack filtered by selector
  • scan(query).filter(fn) → stack filtered by fn.call(element, i)
  • scan(query).filter(element|elements) → stack filtered by one or more elements
  • scan(array).filter(values) → the intersection of 2 arrays

.not(needle)

  • scan(query).filter(selector)nodes filtered against selector
  • scan(query).not(fn) → stack filtered against fn.call(element, i)
  • scan(query).not(element|elements)→ stack filtered against one or more elements
  • scan(array).not(values) → the difference of 2 arrays

#find(needle)

  • scan.find(selector, context?) → array of elements that match selector
  • scan.find(stack, fn, scope?) → the first value to pass fn.call(scope, value, i, stack)

#matches(element, selector)

  • scan.matches(element, selector)true if element matches selector

#contains(haystack, needle)

  • scan.contains(node, element)true if node contains element
  • scan.contains(stack, item, start=0)true if stack contains item
  • scan.contains(str, substr, start=0)true if str contains substr

Support

Selector queries use querySelectorAll where available or else degrade to getElementsByTagName.

Developers

Contribute by making edits in /src or reporting issues.

$ npm install
$ grunt test

Fund

Tip the developer =)

License

MIT

scan's People

Contributors

ryanve avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

scan's Issues

Interoperable $.contains method

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.

scan.prototype

scan() returns a native array in versions 0.5-. For standalone use an array-like instance such that scan(node).find(selector) works and whatnot would be useful. A simple constructor could achieve this.

function scan(item, root) {
    return new Scan(item, root);
}

function Scan(item, root) {
    if (!item) this.length = 0;
    else if (item.nodeType || item.window == item) push.call(this, item);
    else push.apply(this, typeof item == 'string' ? qsa(item, root) : ary(item));
}

scan.fn = scan.prototype = Scan.prototype;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.