Skip to content

Commit 8d20faa

Browse files
committed
Add improved docs
1 parent d7d5849 commit 8d20faa

File tree

2 files changed

+198
-79
lines changed

2 files changed

+198
-79
lines changed

index.js

+30-30
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
44
*/
55

66
/**
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.
129
*
1310
* @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.
1512
* @param {Element} element
1613
* An element.
1714
* @param {number | null | undefined} [index]
18-
* Optionally, its position in its parent.
15+
* The element’s position in its parent.
1916
* @param {Parent | null | undefined} [parent]
20-
* Optionally, its parent.
17+
* The element’s parent.
2118
* @returns {boolean | void}
2219
* Whether this element passes the test.
2320
*/
@@ -26,7 +23,7 @@
2623
* @template {Element} T
2724
* Element type.
2825
* @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.
3027
*/
3128

3229
/**
@@ -39,24 +36,22 @@
3936
* @param {Element} element
4037
* An element.
4138
* @param {number | null | undefined} [index]
42-
* Optionally, its position in its parent.
39+
* The element’s position in its parent.
4340
* @param {Parent | null | undefined} [parent]
44-
* Optionally, its parent.
41+
* The element’s parent.
4542
* @returns {element is T}
4643
* Whether this element passes the test.
4744
*/
4845

4946
/**
50-
* Check if a node is an element and passes a certain node test
51-
*
5247
* @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.
5449
* @param {unknown} [node]
5550
* Anything (typically a node).
5651
* @param {number | null | undefined} [index]
57-
* Optionally, its position in its parent.
52+
* The node’s position in its parent.
5853
* @param {Parent | null | undefined} [parent]
59-
* Optionally, its parent.
54+
* The node’s parent.
6055
* @returns {boolean}
6156
* Whether this is an element and passes a test.
6257
*/
@@ -71,9 +66,9 @@
7166
* @param {unknown} [node]
7267
* Anything (typically a node).
7368
* @param {number | null | undefined} [index]
74-
* Optionally, its position in its parent.
69+
* The node’s position in its parent.
7570
* @param {Parent | null | undefined} [parent]
76-
* Optionally, its parent.
71+
* The node’s parent.
7772
* @returns {node is T}
7873
* Whether this is an element and passes a test.
7974
*/
@@ -82,13 +77,13 @@
8277
* Check if `node` is an `Element` and whether it passes the given test.
8378
*
8479
* @param node
85-
* Anything (typically a node).
80+
* Thing to check, typically `Node`.
8681
* @param test
87-
* A check.
82+
* A check for a specific element.
8883
* @param index
89-
* Optionally, its position in its parent.
84+
* The node’s position in its parent.
9085
* @param parent
91-
* Optionally, its parent.
86+
* The node’s parent.
9287
* @returns
9388
* Whether `node` is an element and passes a test.
9489
*/
@@ -148,16 +143,21 @@ export const isElement =
148143
)
149144

150145
/**
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`.
152153
*
153154
* @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.
158158
* * When `array`, checks any one of the subtests pass.
159159
* @returns
160-
* A test.
160+
* An assertion.
161161
*/
162162
export const convertElement =
163163
/**
@@ -195,7 +195,7 @@ export const convertElement =
195195
/**
196196
* Handle multiple tests.
197197
*
198-
* @param {Array<TagName | TestFunctionAnything>} tests
198+
* @param {Array<string | TestFunctionAnything>} tests
199199
* @returns {AssertAnything}
200200
*/
201201
function anyFactory(tests) {
@@ -230,7 +230,7 @@ function anyFactory(tests) {
230230
/**
231231
* Turn a string into a test for an element with a certain tag name.
232232
*
233-
* @param {TagName} check
233+
* @param {string} check
234234
* @returns {AssertAnything}
235235
*/
236236
function tagNameFactory(check) {

0 commit comments

Comments
 (0)