|
1 | | -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 | | -var parseSelector = require('./lib/selector'), |
4 | | - matchSelector = require('./lib/select'); |
| 3 | +var debug = require('debug')('unist-util-select') |
| 4 | +var parseSelector = require('./lib/selector') |
| 5 | +var matchSelector = require('./lib/select') |
5 | 6 |
|
6 | | -var debug = require('debug')('unist-util-select'); |
| 7 | +module.exports = select |
7 | 8 |
|
| 9 | +select.one = selectOne |
8 | 10 |
|
9 | | -var select = function select (ast, selector) { |
10 | | - if (arguments.length == 1) { |
11 | | - return select.bind(this, ast); |
| 11 | +function select(ast, selector) { |
| 12 | + if (arguments.length === 1) { |
| 13 | + return select.bind(this, ast) |
12 | 14 | } |
13 | 15 |
|
14 | | - debug('Selector: %j', selector); |
15 | | - selector = parseSelector(selector); |
16 | | - debug('AST: %s', |
17 | | - JSON.stringify(selector, null, 2).replace(/(^|\n)/g, '\n ')); |
18 | | - return selector ? matchSelector[selector.type](selector, ast) : []; |
19 | | -}; |
20 | | - |
21 | | - |
22 | | -select.one = function selectOne (ast, selector) { |
23 | | - if (arguments.length == 1) { |
24 | | - return selectOne.bind(this, ast); |
| 16 | + debug('Selector: %j', selector) |
| 17 | + selector = parseSelector(selector) |
| 18 | + debug( |
| 19 | + 'AST: %s', |
| 20 | + JSON.stringify(selector, null, 2).replace(/(^|\n)/g, '\n ') |
| 21 | + ) |
| 22 | + return selector ? matchSelector[selector.type](selector, ast) : [] |
| 23 | +} |
| 24 | + |
| 25 | +function selectOne(ast, selector) { |
| 26 | + if (arguments.length === 1) { |
| 27 | + return selectOne.bind(this, ast) |
25 | 28 | } |
26 | 29 |
|
27 | | - var nodes = select(ast, selector); |
| 30 | + var nodes = select(ast, selector) |
28 | 31 |
|
29 | | - if (!nodes.length) { |
30 | | - throw Error('Node not found by ' + JSON.stringify(selector)); |
| 32 | + if (nodes.length === 0) { |
| 33 | + throw new Error('Node not found by ' + JSON.stringify(selector)) |
31 | 34 | } |
| 35 | + |
32 | 36 | if (nodes.length > 1) { |
33 | | - throw Error('Node matched by ' + JSON.stringify(selector) + ' is not unique'); |
| 37 | + throw new Error( |
| 38 | + 'Node matched by ' + JSON.stringify(selector) + ' is not unique' |
| 39 | + ) |
34 | 40 | } |
35 | 41 |
|
36 | | - return nodes[0]; |
37 | | -}; |
38 | | - |
39 | | - |
40 | | -module.exports = select; |
| 42 | + return nodes[0] |
| 43 | +} |
0 commit comments