Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: syntax-tree/unist-util-select
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d447d05867f93c30f5b4e2b6b6308063275ea34c
Choose a base ref
...
head repository: syntax-tree/unist-util-select
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2ed381f108c6f8c6d51b8c0ac0ba1acf287b4ecb
Choose a head ref
  • 11 commits
  • 33 files changed
  • 2 contributors

Commits on Jun 12, 2017

  1. Update Node in Travis

    wooorm committed Jun 12, 2017
    Copy the full SHA
    ec34f16 View commit details
  2. Refactor markdown

    wooorm committed Jun 12, 2017
    Copy the full SHA
    1a66124 View commit details
  3. Update example in README.md

    wooorm committed Jun 12, 2017
    Copy the full SHA
    9ee2539 View commit details

Commits on Oct 22, 2017

  1. Copy the full SHA
    55a090b View commit details
  2. chore(.dir-locals.el): init

    eush77 committed Oct 22, 2017
    Copy the full SHA
    9de42d9 View commit details
  3. chore(LICENSE): update

    eush77 committed Oct 22, 2017
    Copy the full SHA
    dbbae7c View commit details

Commits on Nov 8, 2018

  1. Update build process

    wooorm committed Nov 8, 2018
    Copy the full SHA
    4c1c17e View commit details
  2. Rewrite library

    wooorm committed Nov 8, 2018
    Copy the full SHA
    22df6d4 View commit details
  3. Rewrite readme

    wooorm committed Nov 8, 2018
    Copy the full SHA
    00eccf9 View commit details
  4. Refactor support

    wooorm committed Nov 8, 2018
    Copy the full SHA
    fb26027 View commit details
  5. 2.0.0

    wooorm committed Nov 8, 2018
    Copy the full SHA
    2ed381f View commit details
Showing with 3,183 additions and 1,258 deletions.
  1. +9 −0 .editorconfig
  2. +6 −1 .gitignore
  3. +1 −0 .prettierignore
  4. +3 −3 .travis.yml
  5. +0 −107 README.md
  6. +15 −35 index.js
  7. +134 −0 lib/any.js
  8. +0 −150 lib/ast-walkers.js
  9. +101 −0 lib/attribute.js
  10. +0 −32 lib/collector.js
  11. +0 −107 lib/match-node.js
  12. +7 −0 lib/name.js
  13. +213 −0 lib/nest.js
  14. +65 −0 lib/parse.js
  15. +212 −0 lib/pseudo.js
  16. +0 −74 lib/select.js
  17. +0 −52 lib/selector.js
  18. +24 −0 lib/test.js
  19. +0 −23 lib/type-index.js
  20. +1 −1 LICENSE → license
  21. +55 −17 package.json
  22. +193 −0 readme.md
  23. +90 −0 test/all.js
  24. +0 −15 test/collector.js
  25. +0 −13 test/curried.js
  26. +8 −0 test/index.js
  27. +0 −224 test/lib/ast.js
  28. +0 −8 test/lib/path.js
  29. +612 −0 test/matches.js
  30. +734 −0 test/select-all.js
  31. +0 −17 test/select-one.js
  32. +700 −352 test/select.js
  33. +0 −27 test/type-index.js
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
/node_modules/
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
yarn.lock
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- 0.12
- 4
- node
- lts/boron
- node
after_script: bash <(curl -s https://codecov.io/bash)
107 changes: 0 additions & 107 deletions README.md

This file was deleted.

50 changes: 15 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
'use strict';
'use strict'

var parseSelector = require('./lib/selector'),
matchSelector = require('./lib/select');
exports.matches = matches
exports.selectAll = selectAll
exports.select = select

var debug = require('debug')('unist-util-select');
var any = require('./lib/any')
var parse = require('./lib/parse')

function matches(selector, node) {
return Boolean(any(parse(selector), node, {one: true, shallow: true})[0])
}

var select = function select (ast, selector) {
if (arguments.length == 1) {
return select.bind(this, ast);
}
function select(selector, node) {
return any(parse(selector), node, {one: true})[0] || null
}

debug('Selector: %j', selector);
selector = parseSelector(selector);
debug('AST: %s',
JSON.stringify(selector, null, 2).replace(/(^|\n)/g, '\n '));
return selector ? matchSelector[selector.type](selector, ast) : [];
};


select.one = function selectOne (ast, selector) {
if (arguments.length == 1) {
return selectOne.bind(this, ast);
}

var nodes = select(ast, selector);

if (!nodes.length) {
throw Error('Node not found by ' + JSON.stringify(selector));
}
if (nodes.length > 1) {
throw Error('Node matched by ' + JSON.stringify(selector) + ' is not unique');
}

return nodes[0];
};


module.exports = select;
function selectAll(selector, node) {
return any(parse(selector), node, {})
}
134 changes: 134 additions & 0 deletions lib/any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
'use strict'

module.exports = match

var zwitch = require('zwitch')
var needsIndex = require('./pseudo').needsIndex
var test = require('./test')
var nest = require('./nest')

var type = zwitch('type')
var handlers = type.handlers

type.unknown = unknownType
type.invalid = invalidType
handlers.selectors = selectors
handlers.ruleSet = ruleSet
handlers.rule = rule

function match(query, node, state) {
return query && node ? type(query, node, state) : []
}

function selectors(query, node, state) {
var collect = collector(state.one)
var ruleSets = query.selectors
var length = ruleSets.length
var index = -1

while (++index < length) {
collect(ruleSet(ruleSets[index], node, state))
}

return collect.result
}

function ruleSet(query, node, state) {
return rule(query.rule, node, state)
}

function rule(query, tree, state) {
var collect = collector(state.one)
var opts = {
scopeNodes: tree.type === 'root' ? tree.children : [tree],
iterator: match,
one: state.one,
shallow: state.shallow
}

if (state.shallow && query.rule) {
throw new Error('Expected selector without nesting')
}

nest(query, tree, 0, null, configure(query, opts))

return collect.result

function match(query, node, index, parent, state) {
if (test(query, node, index, parent, state)) {
if (query.rule) {
nest(query.rule, node, index, parent, configure(query.rule, state))
} else {
collect(node)
state.found = true
}
}
}

function configure(query, state) {
var pseudos = query.pseudos
var length = pseudos && pseudos.length
var index = -1

while (++index < length) {
if (needsIndex.indexOf(pseudos[index].name) !== -1) {
state.index = true
break
}
}

return state
}
}

/* istanbul ignore next - Shouldn’t be invoked, all data is handled. */
function unknownType(query) {
throw new Error('Unknown type `' + query.type + '`')
}

/* istanbul ignore next - Shouldn’t be invoked, parser gives correct data. */
function invalidType() {
throw new Error('Invalid type')
}

function collector(one) {
var result = []
var found

collect.result = result

return collect

/* Append nodes to array, filtering out duplicates. */
function collect(source) {
if ('length' in source) {
collectAll()
} else {
collectOne(source)
}

function collectAll() {
var length = source.length
var index = -1

while (++index < length) {
collectOne(source[index])
}
}

function collectOne(node) {
if (one) {
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */
if (found) {
throw new Error('Cannot collect multiple nodes')
}

found = true
}

if (result.indexOf(node) === -1) {
result.push(node)
}
}
}
}
Loading