1
1
/**
2
2
* @typedef {import('unist').Node } Node
3
3
* @typedef {import('unist').Parent } Parent
4
- *
5
4
* @typedef {string } Type
6
5
* @typedef {Record<string, unknown> } Props
7
- *
8
6
* @typedef {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything> } Test
9
- */
10
-
11
- /**
12
- * Check if a node passes a test
13
7
*
14
8
* @callback TestFunctionAnything
9
+ * Arbitrary function to define whether a node passes.
10
+ * @param {unknown } this
11
+ * The to `is` given `context`
15
12
* @param {Node } node
13
+ * Node to check.
16
14
* @param {number|null|undefined } [index]
15
+ * Index of `node` in `parent`.
17
16
* @param {Parent|null|undefined } [parent]
17
+ * Parent of `node`.
18
18
* @returns {boolean|void }
19
- */
20
-
21
- /**
22
- * Check if a node passes a certain node test
19
+ * Whether `node` matches.
23
20
*
24
- * @template {Node} X
25
- * @callback TestFunctionPredicate
26
- * @param {Node } node
21
+ * @callback AssertAnything
22
+ * Check if a node is an element and passes a certain node test.
23
+ * @param {unknown } [node]
24
+ * Thing to check and check that it’s a node.
27
25
* @param {number|null|undefined } [index]
26
+ * Index of `node` in `parent`.
28
27
* @param {Parent|null|undefined } [parent]
29
- * @returns {node is X }
28
+ * Parent of `node`.
29
+ * @returns {boolean }
30
+ * Whether `node` matches.
30
31
*/
31
32
32
33
/**
33
- * @callback AssertAnything
34
- * @param {unknown } [node]
34
+ * @template {Node} Kind
35
+ * @callback TestFunctionPredicate
36
+ * Arbitrary function to define whether a node passes, using a TypeScript type
37
+ * predicate.
38
+ * @param {Node } node
39
+ * Node to check.
35
40
* @param {number|null|undefined } [index]
41
+ * Index of `node` in `parent`.
36
42
* @param {Parent|null|undefined } [parent]
37
- * @returns {boolean }
43
+ * Parent of `node`.
44
+ * @returns {node is Kind }
45
+ * Whether `node` matches.
38
46
*/
39
47
40
48
/**
41
- * Check if a node passes a certain node test
42
- *
43
- * @template {Node} Y
49
+ * @template {Node} Kind
44
50
* @callback AssertPredicate
51
+ * Check if a node is an element and passes a certain node test, using a
52
+ * TypeScript type predicate.
45
53
* @param {unknown } [node]
54
+ * Thing to check and check that it’s a node.
46
55
* @param {number|null|undefined } [index]
56
+ * Index of `node` in `parent`.
47
57
* @param {Parent|null|undefined } [parent]
48
- * @returns {node is Y }
58
+ * Parent of `node`.
59
+ * @returns {node is Kind }
60
+ * Whether `node` matches.
49
61
*/
50
62
51
63
/**
52
- * Check if a node passes a test.
53
- * When a `parent` node is known the `index` of node should also be given.
64
+ * Check if ` node` passes a test.
65
+ * When a `parent` node is given the `index` of node should also be given.
54
66
*
55
67
* @param node
56
68
* Node to check, can be anything.
57
69
* @param test
58
- * * When nullish, checks if `node` is a `Node`.
59
- * * When `string`, works like passing `(node) => node.type === test`.
60
- * * When `function` checks if function passed the node is true .
61
- * * When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
62
- * * When `array`, checks any one of the subtests pass .
70
+ * * when nullish, checks if `node` is a `Node`.
71
+ * * when `string`, works like passing `(node) => node.type === test`.
72
+ * * when `array`, checks any one of the subtests pass .
73
+ * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
74
+ * * when `function` checks if function passed the node is true .
63
75
* @param index
64
76
* Position of `node` in `parent`, must be a number if `parent` is also given.
65
77
* @param parent
66
78
* Parent of `node`, must be given if `index` is also given.
67
79
* @param context
68
- * Context object to invoke `test` with, optional
80
+ * Context object to call `test` with
69
81
* @returns
70
82
* Whether test passed and `node` is a `Node` (object with `type` set to
71
83
* non-empty `string`).
72
84
*/
73
85
export const is =
74
86
/**
75
87
* @type {(
76
- * (<ExplicitNode extends Node>(node: unknown, test: ExplicitNode ['type']|Partial<ExplicitNode >|TestFunctionPredicate<ExplicitNode >|Array<ExplicitNode ['type']|Partial<ExplicitNode >|TestFunctionPredicate<ExplicitNode >>, index: number, parent: Parent, context?: unknown) => node is ExplicitNode ) &
77
- * (<ExplicitNode extends Node>(node: unknown, test: ExplicitNode ['type']|Partial<ExplicitNode >|TestFunctionPredicate<ExplicitNode >|Array<ExplicitNode ['type']|Partial<ExplicitNode >|TestFunctionPredicate<ExplicitNode >>, index?: null|undefined, parent?: null|undefined, context?: unknown) => node is ExplicitNode ) &
88
+ * (<Kind extends Node>(node: unknown, test: Kind ['type']|Partial<Kind >|TestFunctionPredicate<Kind >|Array<Kind ['type']|Partial<Kind >|TestFunctionPredicate<Kind >>, index: number, parent: Parent, context?: unknown) => node is Kind ) &
89
+ * (<Kind extends Node>(node: unknown, test: Kind ['type']|Partial<Kind >|TestFunctionPredicate<Kind >|Array<Kind ['type']|Partial<Kind >|TestFunctionPredicate<Kind >>, index?: null|undefined, parent?: null|undefined, context?: unknown) => node is Kind ) &
78
90
* ((node: unknown, test: Test, index: number, parent: Parent, context?: unknown) => boolean) &
79
91
* ((node?: unknown, test?: Test, index?: null|undefined, parent?: null|undefined, context?: unknown) => boolean)
80
92
* )}
@@ -124,22 +136,35 @@ export const is =
124
136
}
125
137
)
126
138
139
+ /**
140
+ * Create a test function from `test` that can later be called with a `node`,
141
+ * `index`, and `parent`.
142
+ * Useful if you’re going to test many nodes, for example when creating a
143
+ * utility where something else passes an is-compatible test.
144
+ *
145
+ * The created function is slightly faster that using `is` because it expects
146
+ * valid input only.
147
+ * Therefore, passing invalid input yields unexpected results.
148
+ *
149
+ * @param test
150
+ * * when nullish, checks if `node` is a `Node`.
151
+ * * when `string`, works like passing `(node) => node.type === test`.
152
+ * * when `array`, checks any one of the subtests pass.
153
+ * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
154
+ * * when `function` checks if function passed the node is true.
155
+ * @returns
156
+ * Check function that can be called as `check(node, index, parent)`.
157
+ */
127
158
export const convert =
128
159
/**
129
160
* @type {(
130
- * (<ExplicitNode extends Node>(test: ExplicitNode ['type']|Partial<ExplicitNode >|TestFunctionPredicate<ExplicitNode >) => AssertPredicate<ExplicitNode >) &
161
+ * (<Kind extends Node>(test: Kind ['type']|Partial<Kind >|TestFunctionPredicate<Kind >) => AssertPredicate<Kind >) &
131
162
* ((test?: Test) => AssertAnything)
132
163
* )}
133
164
*/
134
165
(
135
166
/**
136
- * Generate an assertion from a check.
137
167
* @param {Test } [test]
138
- * When nullish, checks if `node` is a `Node`.
139
- * When `string`, works like passing `function (node) {return node.type === test}`.
140
- * When `function` checks if function passed the node is true.
141
- * When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
142
- * When `array`, checks any one of the subtests pass.
143
168
* @returns {AssertAnything }
144
169
*/
145
170
function ( test ) {
@@ -162,6 +187,7 @@ export const convert =
162
187
throw new Error ( 'Expected function, string, or object as test' )
163
188
}
164
189
)
190
+
165
191
/**
166
192
* @param {Array<Type|Props|TestFunctionAnything> } tests
167
193
* @returns {AssertAnything }
@@ -194,8 +220,8 @@ function anyFactory(tests) {
194
220
}
195
221
196
222
/**
197
- * Utility to assert each property in `test` is represented in `node`, and each
198
- * values are strictly equal.
223
+ * Assert each field in `test` is present in `node` and each values are strictly
224
+ * equal.
199
225
*
200
226
* @param {Props } check
201
227
* @returns {AssertAnything }
@@ -221,7 +247,7 @@ function propsFactory(check) {
221
247
}
222
248
223
249
/**
224
- * Utility to convert a string into a function which checks a given node’s type
250
+ * Convert a string into a function which checks a given node’s type
225
251
* for said string.
226
252
*
227
253
* @param {Type } check
@@ -239,8 +265,9 @@ function typeFactory(check) {
239
265
}
240
266
241
267
/**
242
- * Utility to convert a string into a function which checks a given node’s type
268
+ * Convert a string into a function which checks a given node’s type
243
269
* for said string.
270
+ *
244
271
* @param {TestFunctionAnything } check
245
272
* @returns {AssertAnything }
246
273
*/
@@ -258,7 +285,6 @@ function castFactory(check) {
258
285
}
259
286
}
260
287
261
- // Utility to return true.
262
288
function ok ( ) {
263
289
return true
264
290
}
0 commit comments