|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | /**
|
7 |
| - * @typedef {string} TagName |
8 |
| - * Check for a certain tag name. |
9 |
| - * |
10 |
| - * @typedef {null | undefined | TagName | TestFunctionAnything | Array<TagName | TestFunctionAnything>} Test |
11 |
| - * Check for an arbitrary element, unaware of TypeScript. |
| 7 | + * @typedef {null | undefined | string | TestFunctionAnything | Array<string | TestFunctionAnything>} Test |
| 8 | + * Check for an arbitrary element, unaware of TypeScript inferral. |
12 | 9 | *
|
13 | 10 | * @callback TestFunctionAnything
|
14 |
| - * Check if an element passes a test, unaware of TypeScript. |
| 11 | + * Check if an element passes a test, unaware of TypeScript inferral. |
15 | 12 | * @param {Element} element
|
16 | 13 | * An element.
|
17 | 14 | * @param {number | null | undefined} [index]
|
18 |
| - * Optionally, its position in its parent. |
| 15 | + * The element’s position in its parent. |
19 | 16 | * @param {Parent | null | undefined} [parent]
|
20 |
| - * Optionally, its parent. |
| 17 | + * The element’s parent. |
21 | 18 | * @returns {boolean | void}
|
22 | 19 | * Whether this element passes the test.
|
23 | 20 | */
|
|
26 | 23 | * @template {Element} T
|
27 | 24 | * Element type.
|
28 | 25 | * @typedef {T['tagName'] | TestFunctionPredicate<T> | Array<T['tagName'] | TestFunctionPredicate<T>>} PredicateTest
|
29 |
| - * Basic check for an element that can be inferred by TypeScript. |
| 26 | + * Check for an element that can be inferred by TypeScript. |
30 | 27 | */
|
31 | 28 |
|
32 | 29 | /**
|
|
39 | 36 | * @param {Element} element
|
40 | 37 | * An element.
|
41 | 38 | * @param {number | null | undefined} [index]
|
42 |
| - * Optionally, its position in its parent. |
| 39 | + * The element’s position in its parent. |
43 | 40 | * @param {Parent | null | undefined} [parent]
|
44 |
| - * Optionally, its parent. |
| 41 | + * The element’s parent. |
45 | 42 | * @returns {element is T}
|
46 | 43 | * Whether this element passes the test.
|
47 | 44 | */
|
48 | 45 |
|
49 | 46 | /**
|
50 |
| - * Check if a node is an element and passes a certain node test |
51 |
| - * |
52 | 47 | * @callback AssertAnything
|
53 |
| - * Check that an arbitrary value is an element, unaware of TypeScript. |
| 48 | + * Check that an arbitrary value is an element, unaware of TypeScript inferral. |
54 | 49 | * @param {unknown} [node]
|
55 | 50 | * Anything (typically a node).
|
56 | 51 | * @param {number | null | undefined} [index]
|
57 |
| - * Optionally, its position in its parent. |
| 52 | + * The node’s position in its parent. |
58 | 53 | * @param {Parent | null | undefined} [parent]
|
59 |
| - * Optionally, its parent. |
| 54 | + * The node’s parent. |
60 | 55 | * @returns {boolean}
|
61 | 56 | * Whether this is an element and passes a test.
|
62 | 57 | */
|
|
71 | 66 | * @param {unknown} [node]
|
72 | 67 | * Anything (typically a node).
|
73 | 68 | * @param {number | null | undefined} [index]
|
74 |
| - * Optionally, its position in its parent. |
| 69 | + * The node’s position in its parent. |
75 | 70 | * @param {Parent | null | undefined} [parent]
|
76 |
| - * Optionally, its parent. |
| 71 | + * The node’s parent. |
77 | 72 | * @returns {node is T}
|
78 | 73 | * Whether this is an element and passes a test.
|
79 | 74 | */
|
|
82 | 77 | * Check if `node` is an `Element` and whether it passes the given test.
|
83 | 78 | *
|
84 | 79 | * @param node
|
85 |
| - * Anything (typically a node). |
| 80 | + * Thing to check, typically `Node`. |
86 | 81 | * @param test
|
87 |
| - * A check. |
| 82 | + * A check for a specific element. |
88 | 83 | * @param index
|
89 |
| - * Optionally, its position in its parent. |
| 84 | + * The node’s position in its parent. |
90 | 85 | * @param parent
|
91 |
| - * Optionally, its parent. |
| 86 | + * The node’s parent. |
92 | 87 | * @returns
|
93 | 88 | * Whether `node` is an element and passes a test.
|
94 | 89 | */
|
@@ -148,16 +143,21 @@ export const isElement =
|
148 | 143 | )
|
149 | 144 |
|
150 | 145 | /**
|
151 |
| - * Generate an assertion from a check. |
| 146 | + * Generate an assertion from a test. |
| 147 | + * |
| 148 | + * Useful if you’re going to test many nodes, for example when creating a |
| 149 | + * utility where something else passes a compatible test. |
| 150 | + * |
| 151 | + * The created function is a bit faster because it expects valid input only: |
| 152 | + * a `node`, `index`, and `parent`. |
152 | 153 | *
|
153 | 154 | * @param test
|
154 |
| - * * When nullish, checks if `node` is a `Node`. |
155 |
| - * * When `string`, works like passing `function (node) {return node.type === test}`. |
156 |
| - * * When `function` checks if function passed the node is true. |
157 |
| - * * When `object`, checks that all keys in test are in node, and that they have (strictly) equal values. |
| 155 | + * * When nullish, checks if `node` is an `Element`. |
| 156 | + * * When `string`, works like passing `(element) => element.tagName === test`. |
| 157 | + * * When `function` checks if function passed the element is true. |
158 | 158 | * * When `array`, checks any one of the subtests pass.
|
159 | 159 | * @returns
|
160 |
| - * A test. |
| 160 | + * An assertion. |
161 | 161 | */
|
162 | 162 | export const convertElement =
|
163 | 163 | /**
|
@@ -195,7 +195,7 @@ export const convertElement =
|
195 | 195 | /**
|
196 | 196 | * Handle multiple tests.
|
197 | 197 | *
|
198 |
| - * @param {Array<TagName | TestFunctionAnything>} tests |
| 198 | + * @param {Array<string | TestFunctionAnything>} tests |
199 | 199 | * @returns {AssertAnything}
|
200 | 200 | */
|
201 | 201 | function anyFactory(tests) {
|
@@ -230,7 +230,7 @@ function anyFactory(tests) {
|
230 | 230 | /**
|
231 | 231 | * Turn a string into a test for an element with a certain tag name.
|
232 | 232 | *
|
233 |
| - * @param {TagName} check |
| 233 | + * @param {string} check |
234 | 234 | * @returns {AssertAnything}
|
235 | 235 | */
|
236 | 236 | function tagNameFactory(check) {
|
|
0 commit comments