unist-util-select
Advanced tools
Comparing version 1.3.0 to 1.4.0
23
index.js
'use strict'; | ||
var parseSelector = require('./lib/selector'), | ||
select = require('./lib/select'); | ||
matchSelector = require('./lib/select'); | ||
@@ -9,3 +9,3 @@ var debug = require('debug')('unist-util-select'); | ||
module.exports = function (ast, selector) { | ||
var select = function select (ast, selector) { | ||
debug('Selector: %j', selector); | ||
@@ -15,3 +15,20 @@ selector = parseSelector(selector); | ||
JSON.stringify(selector, null, 2).replace(/(^|\n)/g, '\n ')); | ||
return selector ? select[selector.type](selector, ast) : []; | ||
return selector ? matchSelector[selector.type](selector, ast) : []; | ||
}; | ||
select.one = function selectOne (ast, selector) { | ||
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; |
{ | ||
"name": "unist-util-select", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Select unist nodes using css-like selectors", | ||
@@ -48,4 +48,5 @@ "author": "Eugene Sharygin <eush77@gmail.com>", | ||
"devDependencies": { | ||
"tape": "^4.2.0" | ||
"tape": "^4.2.0", | ||
"unist-builder": "^1.0.1" | ||
} | ||
} |
@@ -7,4 +7,6 @@ [![npm](https://nodei.co/npm/unist-util-select.png)](https://npmjs.com/package/unist-util-select) | ||
Select unist nodes using css-like selectors. | ||
Select [Unist] nodes with CSS-like selectors. | ||
[unist]: https://github.com/wooorm/unist | ||
[travis]: https://travis-ci.org/eush77/unist-util-select | ||
@@ -30,3 +32,3 @@ [travis-badge]: https://travis-ci.org/eush77/unist-util-select.svg?branch=master | ||
[`mdast`][mdast] takes this Markdown as an input and returns unist syntax tree. After that, we use `unist-util-select` to extract the required parts: | ||
[remark] takes this Markdown as an input and returns unist syntax tree. After that, we use `unist-util-select` to extract the required parts: | ||
@@ -37,3 +39,3 @@ ```js | ||
var markdown = fs.readFileSync('example.md', 'utf8'); | ||
var ast = mdast.parse(markdown); | ||
var ast = remark.parse(markdown); | ||
@@ -48,6 +50,10 @@ select(ast, 'list text[value*=TODO]') | ||
[mdast]: https://github.com/wooorm/mdast | ||
[remark]: https://github.com/wooorm/remark | ||
## Features | ||
All the relevant parts of [Selectors Level 3][spec]: | ||
[spec]: http://www.w3.org/TR/css3-selectors/ | ||
- [x] Type selectors: `paragraph` | ||
@@ -87,2 +93,8 @@ - [x] Descendant selectors: `paragraph text` | ||
#### `select.one(ast, selector)` | ||
Returns a single node matching `selector`. | ||
Throws an error if node is not found or not unique. | ||
## Install | ||
@@ -89,0 +101,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15910
364
104
2