8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** unist** ] [ unist ] utility to check if a node passes a test.
11
+ [ unist] [ ] utility to check if nodes pass a test.
12
12
13
- ## Install
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When should I use this?] ( #when-should-i-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` is(node[, test[, index, parent[, context]]]) ` ] ( #isnode-test-index-parent-context )
21
+ * [ ` convert(test) ` ] ( #converttest )
22
+ * [ Examples] ( #examples )
23
+ * [ Example of ` convert ` ] ( #example-of-convert )
24
+ * [ Types] ( #types )
25
+ * [ Compatibility] ( #compatibility )
26
+ * [ Related] ( #related )
27
+ * [ Contribute] ( #contribute )
28
+ * [ License] ( #license )
29
+
30
+ ## What is this?
31
+
32
+ This package is a small utility that checks that a node is a certain node.
33
+
34
+ ## When should I use this?
35
+
36
+ Use this small utility if you find yourself repeating code for checking what
37
+ nodes are.
14
38
15
- This package is [ ESM only ] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
16
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d .
39
+ A similar package, [ ` hast-util-is-element ` ] [ hast-util-is-element ] , works on hast
40
+ elements .
17
41
18
- [ npm] [ ] :
42
+ For more advanced tests, [ ` unist-util-select ` ] [ unist-util-select ] can be used
43
+ to match against CSS selectors.
44
+
45
+ ## Install
46
+
47
+ This package is [ ESM only] [ esm ] .
48
+ In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [ npm] [ ] :
19
49
20
50
``` sh
21
51
npm install unist-util-is
22
52
```
23
53
54
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
55
+
56
+ ``` js
57
+ import {is } from " https://esm.sh/unist-util-is@5"
58
+ ```
59
+
60
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
61
+
62
+ ``` html
63
+ <script type =" module" >
64
+ import {is } from " https://esm.sh/unist-util-is@5?bundle"
65
+ </script >
66
+ ```
67
+
24
68
## Use
25
69
26
70
``` js
@@ -29,10 +73,6 @@ import {is} from 'unist-util-is'
29
73
const node = {type: ' strong' }
30
74
const parent = {type: ' paragraph' , children: [node]}
31
75
32
- function test (node , n ) {
33
- return n === 5
34
- }
35
-
36
76
is () // => false
37
77
is ({children: []}) // => false
38
78
is (node) // => true
@@ -46,75 +86,89 @@ is(parent, {type: 'strong'}) // => false
46
86
is (node, test) // => false
47
87
is (node, test, 4 , parent) // => false
48
88
is (node, test, 5 , parent) // => true
89
+
90
+ function test (node , n ) {
91
+ return n === 5
92
+ }
49
93
```
50
94
51
95
## API
52
96
53
- This package exports the following identifiers: ` is ` , ` convert ` .
97
+ This package exports the identifiers ` is ` and ` convert ` .
54
98
There is no default export.
55
99
56
100
### ` is(node[, test[, index, parent[, context]]]) `
57
101
102
+ Check if ` node ` passes a test.
103
+ When a ` parent ` node is given the ` index ` of node should also be given.
104
+
58
105
###### Parameters
59
106
60
- * ` node ` ([ ` Node ` ] [ node ] ) — Node to check.
107
+ * ` node ` ([ ` Node ` ] [ node ] ) — node to check
61
108
* ` test ` ([ ` Function ` ] [ test ] , ` string ` , ` Object ` , or ` Array<Test> ` , optional)
62
- — When nullish, checks if ` node ` is a [ ` Node ` ] [ node ] .
63
- When ` string ` , works like passing ` node => node.type === test ` .
64
- When ` array ` , checks if any one of the subtests pass.
65
- When ` object ` , checks that all keys in ` test ` are in ` node ` ,
66
- and that they have strictly equal values
67
- * ` index ` (` number ` , optional) — [ Index] [ ] of ` node ` in ` parent `
68
- * ` parent ` ([ ` Node ` ] [ node ] , optional) — [ Parent] [ ] of ` node `
69
- * ` context ` (` * ` , optional) — Context object to invoke ` test ` with
109
+ Check.
110
+ * when nullish, checks if ` node ` is a [ ` Node ` ] [ node ]
111
+ * when ` string ` , works like passing ` node => node.type === test `
112
+ * when ` array ` , checks if any one of the subtests pass
113
+ * when ` object ` , checks that all fields in ` test ` are in ` node ` and that
114
+ they have strictly equal values
115
+ * when ` function ` checks if function passed the node is true
116
+ * ` index ` (` number ` , optional) — position of ` node ` in ` parent ` .
117
+ * ` parent ` ([ ` Node ` ] [ node ] , optional) — parent of ` node `
118
+ * ` context ` (` * ` , optional) — context object to call ` test ` with
70
119
71
120
###### Returns
72
121
73
- ` boolean ` — Whether ` test ` passed * and* ` node ` is a [ ` Node ` ] [ node ] (object with
74
- ` type ` set to a non-empty ` string ` ).
122
+ Whether ` test ` passed * and* ` node ` is a [ ` Node ` ] [ node ] (` boolean ` ).
75
123
76
- #### ` function test(node[, index, parent])`
124
+ #### ` test(node[, index, parent]) `
77
125
78
- ###### Parameters
79
-
80
- * ` node ` ([ ` Node ` ] [ node ] ) — Node to check
81
- * ` index ` (` number? ` ) — [ Index] [ ] of ` node ` in ` parent `
82
- * ` parent ` ([ ` Node? ` ] [ node ] ) — [ Parent] [ ] of ` node `
126
+ Arbitrary function to define whether a node passes.
83
127
84
- ###### Context
128
+ ###### Parameters
85
129
86
- ` * ` — The to ` is ` given ` context ` .
130
+ * ` this ` (` * ` ) — the to ` is ` given ` context ` .
131
+ * ` node ` ([ ` Node ` ] [ node ] ) — node to check
132
+ * ` index ` (` number? ` ) — [ index] [ ] of ` node ` in ` parent `
133
+ * ` parent ` ([ ` Node? ` ] [ node ] ) — [ parent] [ ] of ` node `
87
134
88
135
###### Returns
89
136
90
- ` boolean? ` — Whether ` node ` matches.
137
+ Whether ` node ` matches ( ` boolean? ` ) .
91
138
92
139
### ` convert(test) `
93
140
94
- Create a test function from ` test ` , that can later be called with a ` node ` ,
141
+ Create a test function from ` test ` that can later be called with a ` node ` ,
95
142
` index ` , and ` parent ` .
96
143
Useful if you’re going to test many nodes, for example when creating a utility
97
144
where something else passes an is-compatible test.
98
145
99
- The created function is slightly faster because it expects valid input only.
100
- Therefore, passing invalid input, yields unexpected results.
146
+ The created function is slightly faster that using ` is ` because it expects valid
147
+ input only.
148
+ Therefore, passing invalid input yields unexpected results.
101
149
102
- For example:
150
+ ###### Returns
151
+
152
+ Check function that can be called as ` check(node, index, parent) ` .
153
+
154
+ ## Examples
155
+
156
+ ### Example of ` convert `
103
157
104
158
``` js
105
- import u from ' unist-builder'
159
+ import { u } from ' unist-builder'
106
160
import {convert } from ' unist-util-is'
107
161
108
- var test = convert (' leaf' )
162
+ const test = convert (' leaf' )
109
163
110
- var tree = u (' tree' , [
164
+ const tree = u (' tree' , [
111
165
u (' node' , [u (' leaf' , ' 1' )]),
112
166
u (' leaf' , ' 2' ),
113
167
u (' node' , [u (' leaf' , ' 3' ), u (' leaf' , ' 4' )]),
114
168
u (' leaf' , ' 5' )
115
169
])
116
170
117
- var leafs = tree .children .filter ((child , index ) => test (child, index, tree))
171
+ const leafs = tree .children .filter ((child , index ) => test (child, index, tree))
118
172
119
173
console .log (leafs)
120
174
```
@@ -125,27 +179,50 @@ Yields:
125
179
[{type: ' leaf' , value: ' 2' }, {type: ' leaf' , value: ' 5' }]
126
180
```
127
181
182
+ ## Types
183
+
184
+ This package is fully typed with [ TypeScript] [ ] .
185
+ It exports the additional types:
186
+
187
+ * ` Test `
188
+ — models any arbitrary test that can be given
189
+ * ` TestFunctionAnything `
190
+ — models any test function
191
+ * ` TestFunctionPredicate<Kind> ` (where ` Kind ` extends ` Node ` )
192
+ — models a test function for ` Kind `
193
+ * ` AssertAnything `
194
+ — models a check function as returned by ` convert `
195
+ * ` AssertPredicate<Kind> ` (where ` Kind ` extends ` Node ` )
196
+ — models a check function for ` Kind ` as returned by ` convert `
197
+
198
+ ## Compatibility
199
+
200
+ Projects maintained by the unified collective are compatible with all maintained
201
+ versions of Node.js.
202
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
203
+ Our projects sometimes work with older versions, but this is not guaranteed.
204
+
128
205
## Related
129
206
130
207
* [ ` unist-util-find-after ` ] ( https://github.com/syntax-tree/unist-util-find-after )
131
- — Find a node after another node
208
+ — find a node after another node
132
209
* [ ` unist-util-find-before ` ] ( https://github.com/syntax-tree/unist-util-find-before )
133
- — Find a node before another node
210
+ — find a node before another node
134
211
* [ ` unist-util-find-all-after ` ] ( https://github.com/syntax-tree/unist-util-find-all-after )
135
- — Find all nodes after another node
212
+ — find all nodes after another node
136
213
* [ ` unist-util-find-all-before ` ] ( https://github.com/syntax-tree/unist-util-find-all-before )
137
- — Find all nodes before another node
214
+ — find all nodes before another node
138
215
* [ ` unist-util-find-all-between ` ] ( https://github.com/mrzmmr/unist-util-find-all-between )
139
- — Find all nodes between two nodes
216
+ — find all nodes between two nodes
140
217
* [ ` unist-util-filter ` ] ( https://github.com/syntax-tree/unist-util-filter )
141
- — Create a new tree with nodes that pass a check
218
+ — create a new tree with nodes that pass a check
142
219
* [ ` unist-util-remove ` ] ( https://github.com/syntax-tree/unist-util-remove )
143
- — Remove nodes from tree
220
+ — remove nodes from tree
144
221
145
222
## Contribute
146
223
147
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
148
- started.
224
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
225
+ ways to get started.
149
226
See [ ` support.md ` ] [ support ] for ways to get help.
150
227
151
228
This project has a [ code of conduct] [ coc ] .
@@ -186,15 +263,23 @@ abide by its terms.
186
263
187
264
[ npm ] : https://docs.npmjs.com/cli/install
188
265
266
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
267
+
268
+ [ esmsh ] : https://esm.sh
269
+
270
+ [ typescript ] : https://www.typescriptlang.org
271
+
189
272
[ license ] : license
190
273
191
274
[ author ] : https://wooorm.com
192
275
193
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
276
+ [ health ] : https://github.com/syntax-tree/.github
277
+
278
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
194
279
195
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
280
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
196
281
197
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD /code-of-conduct.md
282
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main /code-of-conduct.md
198
283
199
284
[ unist ] : https://github.com/syntax-tree/unist
200
285
@@ -204,4 +289,8 @@ abide by its terms.
204
289
205
290
[ index ] : https://github.com/syntax-tree/unist#index
206
291
207
- [ test ] : #function-testnode-index-parent
292
+ [ hast-util-is-element ] : https://github.com/syntax-tree/hast-util-is-element
293
+
294
+ [ unist-util-select ] : https://github.com/syntax-tree/unist-util-select
295
+
296
+ [ test ] : #testnode-index-parent
0 commit comments