Skip to content

Commit 31d6a76

Browse files
committed
Refactor code-style
* Add more docs to JSDoc
1 parent 7c74344 commit 31d6a76

File tree

1 file changed

+86
-46
lines changed

1 file changed

+86
-46
lines changed

index.js

+86-46
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,99 @@
11
/**
2-
* @typedef {import('unist').Node} Node
32
* @typedef {import('unist').Parent} Parent
43
* @typedef {import('hast').Element} Element
5-
*
4+
*/
5+
6+
/**
67
* @typedef {string} TagName
8+
* Check for a certain tag name.
79
*
8-
* @typedef {null|undefined|TagName|TestFunctionAnything|Array<TagName|TestFunctionAnything>} Test
10+
* @typedef {null | undefined | TagName | TestFunctionAnything | Array<TagName | TestFunctionAnything>} Test
11+
* Check for an arbitrary element, unaware of TypeScript.
912
*
1013
* @callback TestFunctionAnything
11-
* Check if an element passes a test.
14+
* Check if an element passes a test, unaware of TypeScript.
1215
* @param {Element} element
13-
* @param {number|null|undefined} [index]
14-
* @param {Parent|null|undefined} [parent]
15-
* @returns {boolean|void}
16+
* An element.
17+
* @param {number | null | undefined} [index]
18+
* Optionally, its position in its parent.
19+
* @param {Parent | null | undefined} [parent]
20+
* Optionally, its parent.
21+
* @returns {boolean | void}
22+
* Whether this element passes the test.
1623
*/
1724

1825
/**
1926
* @template {Element} T
20-
* @typedef {null|undefined|T['tagName']|TestFunctionPredicate<T>|Array<T['tagName']|TestFunctionPredicate<T>>} PredicateTest
27+
* Element type.
28+
* @typedef {T['tagName'] | TestFunctionPredicate<T> | Array<T['tagName'] | TestFunctionPredicate<T>>} PredicateTest
29+
* Basic check for an element that can be inferred by TypeScript.
2130
*/
2231

2332
/**
2433
* Check if an element passes a certain node test.
2534
*
26-
* @template {Element} X
35+
* @template {Element} T
36+
* Element type.
2737
* @callback TestFunctionPredicate
38+
* Complex test function for an element that can be inferred by TypeScript.
2839
* @param {Element} element
29-
* @param {number|null|undefined} [index]
30-
* @param {Parent|null|undefined} [parent]
31-
* @returns {element is X}
40+
* An element.
41+
* @param {number | null | undefined} [index]
42+
* Optionally, its position in its parent.
43+
* @param {Parent | null | undefined} [parent]
44+
* Optionally, its parent.
45+
* @returns {element is T}
46+
* Whether this element passes the test.
3247
*/
3348

3449
/**
3550
* Check if a node is an element and passes a certain node test
3651
*
3752
* @callback AssertAnything
53+
* Check that an arbitrary value is an element, unaware of TypeScript.
3854
* @param {unknown} [node]
39-
* @param {number|null|undefined} [index]
40-
* @param {Parent|null|undefined} [parent]
55+
* Anything (typically a node).
56+
* @param {number | null | undefined} [index]
57+
* Optionally, its position in its parent.
58+
* @param {Parent | null | undefined} [parent]
59+
* Optionally, its parent.
4160
* @returns {boolean}
61+
* Whether this is an element and passes a test.
4262
*/
4363

4464
/**
4565
* Check if a node is an element and passes a certain node test
4666
*
47-
* @template {Element} Y
67+
* @template {Element} T
68+
* Element type.
4869
* @callback AssertPredicate
70+
* Check that an arbitrary value is a specific element, aware of TypeScript.
4971
* @param {unknown} [node]
50-
* @param {number|null|undefined} [index]
51-
* @param {Parent|null|undefined} [parent]
52-
* @returns {node is Y}
72+
* Anything (typically a node).
73+
* @param {number | null | undefined} [index]
74+
* Optionally, its position in its parent.
75+
* @param {Parent | null | undefined} [parent]
76+
* Optionally, its parent.
77+
* @returns {node is T}
78+
* Whether this is an element and passes a test.
5379
*/
5480

55-
// Check if `node` is an `element` and whether it passes the given test.
81+
/**
82+
* Check if `node` is an `Element` and whether it passes the given test.
83+
*
84+
* @param node
85+
* Anything (typically a node).
86+
* @param test
87+
* A check.
88+
* @param index
89+
* Optionally, its position in its parent.
90+
* @param parent
91+
* Optionally, its parent.
92+
* @returns
93+
* Whether `node` is an element and passes a test.
94+
*/
5695
export const isElement =
5796
/**
58-
* Check if a node is an element and passes a test.
59-
* When a `parent` node is known the `index` of node should also be given.
60-
*
6197
* @type {(
6298
* (() => false) &
6399
* (<T extends Element = Element>(node: unknown, test?: PredicateTest<T>, index?: number, parent?: Parent, context?: unknown) => node is T) &
@@ -66,18 +102,12 @@ export const isElement =
66102
*/
67103
(
68104
/**
69-
* Check if a node passes a test.
70-
* When a `parent` node is known the `index` of node should also be given.
71-
*
72-
* @param {unknown} [node] Node to check
73-
* @param {Test} [test] When nullish, checks if `node` is a `Node`.
74-
* When `string`, works like passing `function (node) {return node.type === test}`.
75-
* When `function` checks if function passed the node is true.
76-
* When `array`, checks any one of the subtests pass.
77-
* @param {number} [index] Position of `node` in `parent`
78-
* @param {Parent} [parent] Parent of `node`
79-
* @param {unknown} [context] Context object to invoke `test` with
80-
* @returns {boolean} Whether test passed and `node` is an `Element` (object with `type` set to `element` and `tagName` set to a non-empty string).
105+
* @param {unknown} [node]
106+
* @param {Test | undefined} [test]
107+
* @param {number | null | undefined} [index]
108+
* @param {Parent | null | undefined} [parent]
109+
* @param {unknown} [context]
110+
* @returns {boolean}
81111
*/
82112
// eslint-disable-next-line max-params
83113
function (node, test, index, parent, context) {
@@ -117,22 +147,28 @@ export const isElement =
117147
}
118148
)
119149

150+
/**
151+
* Generate an assertion from a check.
152+
*
153+
* @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.
158+
* * When `array`, checks any one of the subtests pass.
159+
* @returns
160+
* A test.
161+
*/
120162
export const convertElement =
121163
/**
122164
* @type {(
123-
* (<T extends Element>(test: T['tagName']|TestFunctionPredicate<T>) => AssertPredicate<T>) &
165+
* (<T extends Element>(test: T['tagName'] | TestFunctionPredicate<T>) => AssertPredicate<T>) &
124166
* ((test?: Test) => AssertAnything)
125167
* )}
126168
*/
127169
(
128170
/**
129-
* Generate an assertion from a check.
130-
* @param {Test} [test]
131-
* When nullish, checks if `node` is a `Node`.
132-
* When `string`, works like passing `function (node) {return node.type === test}`.
133-
* When `function` checks if function passed the node is true.
134-
* When `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
135-
* When `array`, checks any one of the subtests pass.
171+
* @param {Test | null | undefined} [test]
136172
* @returns {AssertAnything}
137173
*/
138174
function (test) {
@@ -157,7 +193,9 @@ export const convertElement =
157193
)
158194

159195
/**
160-
* @param {Array<TagName|TestFunctionAnything>} tests
196+
* Handle multiple tests.
197+
*
198+
* @param {Array<TagName | TestFunctionAnything>} tests
161199
* @returns {AssertAnything}
162200
*/
163201
function anyFactory(tests) {
@@ -190,8 +228,7 @@ function anyFactory(tests) {
190228
}
191229

192230
/**
193-
* Utility to convert a string into a function which checks a given node’s tag
194-
* name for said string.
231+
* Turn a string into a test for an element with a certain tag name.
195232
*
196233
* @param {TagName} check
197234
* @returns {AssertAnything}
@@ -209,6 +246,8 @@ function tagNameFactory(check) {
209246
}
210247

211248
/**
249+
* Turn a custom test into a test for an element that passes that test.
250+
*
212251
* @param {TestFunctionAnything} check
213252
* @returns {AssertAnything}
214253
*/
@@ -228,7 +267,8 @@ function castFactory(check) {
228267
}
229268

230269
/**
231-
* Utility to return true if this is an element.
270+
* Make sure something is an element.
271+
*
232272
* @param {unknown} node
233273
* @returns {node is Element}
234274
*/

0 commit comments

Comments
 (0)