Skip to content

Commit 230f8df

Browse files
committed
Add docs to types
1 parent 94482b3 commit 230f8df

File tree

3 files changed

+102
-29
lines changed

3 files changed

+102
-29
lines changed

index.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,57 @@ import {any} from './lib/any.js'
88
import {parse} from './lib/parse.js'
99

1010
/**
11+
* Check that the given `node` matches `selector`.
12+
*
13+
* This only checks the node itself, not the surrounding tree.
14+
* Thus, nesting in selectors is not supported (`paragraph strong`,
15+
* `paragraph > strong`), neither are selectors like `:first-child`, etc.
16+
* This only checks that the given node matches the selector.
17+
*
1118
* @param {string} selector
12-
* @param {NodeLike|Node} [node]
19+
* CSS selector, such as (`heading`, `link, linkReference`).
20+
* @param {Node | NodeLike | undefined} [node]
21+
* Node that might match `selector`.
1322
* @returns {boolean}
23+
* Whether `node` matches `selector`.
1424
*/
1525
export function matches(selector, node) {
1626
return Boolean(any(parse(selector), node, {one: true, shallow: true, any})[0])
1727
}
1828

1929
/**
30+
* Select the first node that matches `selector` in the given `tree`.
31+
*
32+
* Searches the tree in *preorder*.
33+
*
2034
* @param {string} selector
21-
* @param {NodeLike|Node} [node]
22-
* @returns {Node|null}
35+
* CSS selector, such as (`heading`, `link, linkReference`).
36+
* @param {Node | NodeLike | undefined} [tree]
37+
* Tree to search.
38+
* @returns {Node | null}
39+
* First node in `tree` that matches `selector` or `null` if nothing is
40+
* found.
41+
*
42+
* This could be `tree` itself.
2343
*/
24-
export function select(selector, node) {
25-
return any(parse(selector), node, {one: true, any})[0] || null
44+
export function select(selector, tree) {
45+
return any(parse(selector), tree, {one: true, any})[0] || null
2646
}
2747

2848
/**
49+
* Select all nodes that match `selector` in the given `tree`.
50+
*
51+
* Searches the tree in *preorder*.
52+
*
2953
* @param {string} selector
30-
* @param {NodeLike|Node} [node]
54+
* CSS selector, such as (`heading`, `link, linkReference`).
55+
* @param {Node | NodeLike | undefined} [tree]
56+
* Tree to search.
3157
* @returns {Array<Node>}
58+
* Nodes in `tree` that match `selector`.
59+
*
60+
* This could include `tree` itself.
3261
*/
33-
export function selectAll(selector, node) {
34-
return any(parse(selector), node, {any})
62+
export function selectAll(selector, tree) {
63+
return any(parse(selector), tree, {any})
3564
}

lib/types.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Parent} Parent
4+
*
25
* @typedef {import('css-selector-parser').Selector} Selector
36
* @typedef {import('css-selector-parser').Selectors} Selectors
47
* @typedef {import('css-selector-parser').RuleSet} RuleSet
@@ -7,29 +10,26 @@
710
* @typedef {import('css-selector-parser').AttrValueType} AttrValueType
811
* @typedef {Selector|Rule|RulePseudo} Query
912
*
10-
* Fix for types.
11-
* @typedef {Object} RuleAttr
13+
* @typedef RuleAttr
14+
* Fix for types from `css-selector-parser`.
1215
* @property {string} name
1316
* @property {string} [operator]
1417
* @property {AttrValueType} [valueType]
1518
* @property {string} [value]
1619
*
17-
* More specific type for registered selector pseudos.
18-
* @typedef {Object} RulePseudoSelector
20+
* @typedef RulePseudoSelector
21+
* More specific type for registered selector pseudos.
1922
* @property {string} name
2023
* @property {'selector'} valueType
2124
* @property {Selector} value
2225
*
23-
* Overwrite to compile nth-checks once.
24-
* @typedef {Object} RulePseudoNth
26+
* @typedef RulePseudoNth
27+
* Overwrite to compile nth-checks once.
2528
* @property {string} name
2629
* @property {'function'} valueType
2730
* @property {(index: number) => boolean} value
2831
*
29-
* @typedef {import('unist').Node} Node
30-
* @typedef {import('unist').Parent} Parent
31-
*
32-
* @typedef {Object} SelectState
32+
* @typedef SelectState
3333
* @property {(query: Selectors|RuleSet|Rule, node: Node|undefined, state: SelectState) => Node[]} any
3434
* @property {SelectIterator|null|undefined} [iterator]
3535
* @property {Array<Node>} [scopeNodes]

readme.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,25 @@ There is no default export.
3535

3636
### `matches(selector, node)`
3737

38-
Check that the given [node][] matches `selector`.
39-
Returns `boolean`, whether the node matches or not.
38+
Check that the given `node` matches `selector`.
4039

41-
This only checks the element itself, not the surrounding tree.
40+
This only checks the node itself, not the surrounding tree.
4241
Thus, nesting in selectors is not supported (`paragraph strong`,
43-
`paragraph > strong`), nor are selectors like `:first-child`, etc.
44-
This only checks that the given element matches the selector.
42+
`paragraph > strong`), neither are selectors like `:first-child`, etc.
43+
This only checks that the given node matches the selector.
44+
45+
###### Parameters
46+
47+
* `selector` (`string`)
48+
— CSS selector, such as (`heading`, `link, linkReference`).
49+
* `node` ([`Node`][node], optional)
50+
— node that might match `selector`
51+
52+
###### Returns
53+
54+
Whether `node` matches `selector` (`boolean`).
55+
56+
###### Example
4557

4658
```js
4759
import {u} from 'unist-builder'
@@ -53,9 +65,24 @@ matches('[lang]', u('code', {lang: 'js'}, 'console.log(1)')) // => true
5365

5466
### `select(selector, tree)`
5567

56-
Select the first node matching `selector` in the given `tree` (could be the
57-
tree itself).
58-
Returns the found [node][], if any.
68+
Select the first node that matches `selector` in the given `tree`.
69+
70+
Searches the tree in *[preorder][]*.
71+
72+
###### Parameters
73+
74+
* `selector` (`string`)
75+
— CSS selector, such as (`heading`, `link, linkReference`).
76+
* `tree` ([`Node`][node], optional)
77+
— tree to search
78+
79+
###### Returns
80+
81+
First node in `tree` that matches `selector` or `null` if nothing is found.
82+
83+
This could be `tree` itself.
84+
85+
###### Example
5986

6087
```js
6188
import {u} from 'unist-builder'
@@ -83,9 +110,24 @@ Yields:
83110

84111
### `selectAll(selector, tree)`
85112

86-
Select all nodes matching `selector` in the given `tree` (could include the
87-
tree itself).
88-
Returns all found [node][]s, if any.
113+
Select all nodes that match `selector` in the given `tree`.
114+
115+
Searches the tree in *[preorder][]*.
116+
117+
###### Parameters
118+
119+
* `selector` (`string`)
120+
— CSS selector, such as (`heading`, `link, linkReference`).
121+
* `tree` ([`Node`][node], optional)
122+
— tree to search
123+
124+
###### Returns
125+
126+
Nodes in `tree` that match `selector`.
127+
128+
This could include `tree` itself.
129+
130+
###### Example
89131

90132
```js
91133
import {u} from 'unist-builder'
@@ -232,6 +274,8 @@ abide by its terms.
232274

233275
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
234276

277+
[preorder]: https://github.com/syntax-tree/unist#preorder
278+
235279
[unist]: https://github.com/syntax-tree/unist
236280

237281
[node]: https://github.com/syntax-tree/unist#node

0 commit comments

Comments
 (0)