19
19
* [ API] ( #api )
20
20
* [ ` is(node[, test[, index, parent[, context]]]) ` ] ( #isnode-test-index-parent-context )
21
21
* [ ` convert(test) ` ] ( #converttest )
22
+ * [ ` AssertAnything ` ] ( #assertanything )
23
+ * [ ` AssertPredicate ` ] ( #assertpredicate )
24
+ * [ ` Test ` ] ( #test )
25
+ * [ ` TestFunctionAnything ` ] ( #testfunctionanything )
26
+ * [ ` PredicateTest ` ] ( #predicatetest )
27
+ * [ ` TestFunctionPredicate ` ] ( #testfunctionpredicate )
22
28
* [ Examples] ( #examples )
23
29
* [ Example of ` convert ` ] ( #example-of-convert )
24
30
* [ Types] ( #types )
@@ -45,7 +51,7 @@ to match against CSS selectors.
45
51
## Install
46
52
47
53
This package is [ ESM only] [ esm ] .
48
- In Node.js (version 12.20+, 14.14+, 16.0+, 18 .0+), install with [ npm] [ ] :
54
+ In Node.js (version 14.14+ and 16 .0+), install with [ npm] [ ] :
49
55
50
56
``` sh
51
57
npm install unist-util-is
@@ -54,14 +60,14 @@ npm install unist-util-is
54
60
In Deno with [ ` esm.sh ` ] [ esmsh ] :
55
61
56
62
``` js
57
- import {is } from " https://esm.sh/unist-util-is@5"
63
+ import {is } from ' https://esm.sh/unist-util-is@5'
58
64
```
59
65
60
66
In browsers with [ ` esm.sh ` ] [ esmsh ] :
61
67
62
68
``` html
63
69
<script type =" module" >
64
- import {is } from " https://esm.sh/unist-util-is@5?bundle"
70
+ import {is } from ' https://esm.sh/unist-util-is@5?bundle'
65
71
</script >
66
72
```
67
73
@@ -94,62 +100,176 @@ function test(node, n) {
94
100
95
101
## API
96
102
97
- This package exports the identifiers ` is ` and ` convert ` .
103
+ This package exports the identifiers [ ` convert ` ] [ convert ] and [ ` is ` ] [ is ] .
98
104
There is no default export.
99
105
100
106
### ` is(node[, test[, index, parent[, context]]]) `
101
107
102
- Check if ` node ` passes a test.
103
- When a ` parent ` node is given the ` index ` of node should also be given.
108
+ Check if ` node ` is a ` Node ` and whether it passes the given test.
104
109
105
110
###### Parameters
106
111
107
- * ` node ` ([ ` Node ` ] [ node ] ) — node to check
108
- * ` test ` ([ ` Function ` ] [ test ] , ` string ` , ` Object ` , or ` Array<Test> ` , optional)
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
112
+ * ` node ` (` unknown ` )
113
+ — thing to check, typically [ ` Node ` ] [ node ]
114
+ * ` test ` ([ ` Test ` ] [ test ] or [ ` PredicateTest ` ] [ predicatetest ] , optional)
115
+ — a check for a specific element
116
+ * ` index ` (` number ` , optional)
117
+ — the node’s position in its parent
118
+ * ` parent ` ([ ` Node ` ] [ node ] , optional)
119
+ — the node’s parent
120
+ * ` context ` (` any ` , optional)
121
+ — context object (` this ` ) to call ` test ` with
119
122
120
123
###### Returns
121
124
122
- Whether ` test ` passed * and * ` node ` is a [ ` Node ` ] [ node ] (` boolean ` ).
125
+ Whether ` node ` is a [ ` Node ` ] [ node ] and passes a test (` boolean ` ).
123
126
124
- #### ` test(node[, index, parent]) `
127
+ ###### Throws
125
128
126
- Arbitrary function to define whether a node passes.
129
+ When an incorrect ` test ` , ` index ` , or ` parent ` is given.
130
+ There is no error thrown when ` node ` is not a node.
131
+
132
+ ### ` convert(test) `
133
+
134
+ Generate a check from a test.
135
+
136
+ Useful if you’re going to test many nodes, for example when creating a
137
+ utility where something else passes a compatible test.
138
+
139
+ The created function is a bit faster because it expects valid input only:
140
+ a ` node ` , ` index ` , and ` parent ` .
127
141
128
142
###### Parameters
129
143
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 `
144
+ * ` test ` ([ ` Test ` ] [ test ] or [ ` PredicateTest ` ] [ predicatetest ] , optional)
145
+ — a check for a specific node
134
146
135
147
###### Returns
136
148
137
- Whether ` node ` matches (` boolean? ` ).
149
+ An assertion ([ ` AssertAnything ` ] [ assertanything ] or
150
+ [ ` AssertPredicate ` ] [ assertpredicate ] ).
138
151
139
- ### ` convert(test) `
152
+ ### ` AssertAnything `
153
+
154
+ Check that an arbitrary value is a node, unaware of TypeScript inferral
155
+ (TypeScript type).
156
+
157
+ ###### Parameters
158
+
159
+ * ` node ` (` unknown ` )
160
+ — anything (typically a node)
161
+ * ` index ` (` number ` , optional)
162
+ — the node’s position in its parent
163
+ * ` parent ` ([ ` Node ` ] [ node ] , optional)
164
+ — the node’s parent
165
+
166
+ ###### Returns
167
+
168
+ Whether this is a node and passes a test (` boolean ` ).
169
+
170
+ ### ` AssertPredicate `
171
+
172
+ Check that an arbitrary value is a specific node, aware of TypeScript
173
+ (TypeScript type).
174
+
175
+ ###### Type parameters
176
+
177
+ * ` Kind ` ([ ` Node ` ] [ node ] )
178
+ — node type
179
+
180
+ ###### Parameters
181
+
182
+ * ` node ` (` unknown ` )
183
+ — anything (typically a node)
184
+ * ` index ` (` number ` , optional)
185
+ — the node’s position in its parent
186
+ * ` parent ` ([ ` Node ` ] [ node ] , optional)
187
+ — the node’s parent
188
+
189
+ ###### Returns
190
+
191
+ Whether this is a node and passes a test (` node is Kind ` ).
192
+
193
+ ### ` Test `
194
+
195
+ Check for an arbitrary node, unaware of TypeScript inferral (TypeScript
196
+ type).
197
+
198
+ ###### Type
199
+
200
+ ``` ts
201
+ type Test =
202
+ | null
203
+ | undefined
204
+ | string
205
+ | Record <string , unknown >
206
+ | TestFunctionAnything
207
+ | Array <string | Record <string , unknown > | TestFunctionAnything >
208
+ ` ` `
209
+
210
+ Checks that the given thing is a node, and then:
140
211
141
- Create a test function from ` test ` that can later be called with a ` node ` ,
142
- ` index ` , and ` parent ` .
143
- Useful if you’re going to test many nodes, for example when creating a utility
144
- where something else passes an is-compatible test.
212
+ * when ` string ` , checks that the node has that tag name
213
+ * when ` function ` , see [ ` TestFunctionAnything ` ][testfunctionanything]
214
+ * when ` object ` , checks that all keys in test are in node, and that they have
215
+ (strictly) equal values
216
+ * when ` Array ` , checks if one of the subtests pass
145
217
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.
218
+ ### ` TestFunctionAnything `
219
+
220
+ Check if a node passes a test, unaware of TypeScript inferral (TypeScript
221
+ type).
222
+
223
+ ###### Parameters
224
+
225
+ * ` node ` ([ ` Node ` ][node])
226
+ — a node
227
+ * ` index ` ( ` number ` , optional)
228
+ — the node’s position in its parent
229
+ * ` parent ` ([ ` Node ` ][node], optional)
230
+ — the node’s parent
149
231
150
232
###### Returns
151
233
152
- Check function that can be called as ` check(node, index, parent) ` .
234
+ Whether this node passes the test ( ` boolean ` ).
235
+
236
+ ### ` PredicateTest `
237
+
238
+ Check for a node that can be inferred by TypeScript (TypeScript type).
239
+
240
+ ###### Type
241
+
242
+ ` ` ` ts
243
+ type PredicateTest <Kind extends Node > =
244
+ | Kind [' type' ]
245
+ | Partial <Kind >
246
+ | TestFunctionPredicate <Kind >
247
+ | Array <Kind [' type' ] | Partial <Kind > | TestFunctionPredicate <Kind >>
248
+ ` ` `
249
+
250
+ See [ ` TestFunctionPredicate ` ][testfunctionpredicate].
251
+
252
+ ### ` TestFunctionPredicate `
253
+
254
+ Check if a node passes a certain node test (TypeScript type).
255
+
256
+ ###### Type parameters
257
+
258
+ * ` Kind ` ([ ` Node ` ][node])
259
+ — node type
260
+
261
+ ###### Parameters
262
+
263
+ * ` node ` ([ ` Node ` ][node])
264
+ — a node
265
+ * ` index ` ( ` number ` , optional)
266
+ — the node’s position in its parent
267
+ * ` parent ` ([ ` Node ` ][node], optional)
268
+ — the node’s parent
269
+
270
+ ###### Returns
271
+
272
+ Whether this node passes the test ( ` node is Kind ` ).
153
273
154
274
## Examples
155
275
@@ -182,24 +302,17 @@ Yields:
182
302
## Types
183
303
184
304
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 `
305
+ It exports the additional types [ ` AssertAnything ` ] [ assertanything ] ,
306
+ [ ` AssertPredicate ` ] [ assertpredicate ] , [ ` Test ` ] [ test ] ,
307
+ [ ` TestFunctionAnything ` ] [ testfunctionanything ] ,
308
+ [ ` TestFunctionPredicate ` ] [ testfunctionpredicate ] , and
309
+ [ ` PredicateTest ` ] [ predicatetest ] .
197
310
198
311
## Compatibility
199
312
200
313
Projects maintained by the unified collective are compatible with all maintained
201
314
versions of Node.js.
202
- As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18 .0+.
315
+ As of now, that is Node.js 14.14+ and 16 .0+.
203
316
Our projects sometimes work with older versions, but this is not guaranteed.
204
317
205
318
## Related
@@ -285,12 +398,22 @@ abide by its terms.
285
398
286
399
[ node ] : https://github.com/syntax-tree/unist#node
287
400
288
- [ parent ] : https://github.com/syntax-tree/unist#parent-1
289
-
290
- [ index ] : https://github.com/syntax-tree/unist#index
291
-
292
401
[ hast-util-is-element ] : https://github.com/syntax-tree/hast-util-is-element
293
402
294
403
[ unist-util-select ] : https://github.com/syntax-tree/unist-util-select
295
404
296
- [ test ] : #testnode-index-parent
405
+ [ is ] : #isnode-test-index-parent-context
406
+
407
+ [ convert ] : #converttest
408
+
409
+ [ assertanything ] : #assertanything
410
+
411
+ [ assertpredicate ] : #assertpredicate
412
+
413
+ [ test ] : #test
414
+
415
+ [ testfunctionanything ] : #testfunctionanything
416
+
417
+ [ testfunctionpredicate ] : #testfunctionpredicate
418
+
419
+ [ predicatetest ] : #predicatetest
0 commit comments