Skip to content

Commit 8971e05

Browse files
committed
Refactor code-style
1 parent 1bd27b2 commit 8971e05

File tree

2 files changed

+215
-193
lines changed

2 files changed

+215
-193
lines changed

lib/index.js

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,56 @@ import {convert} from 'unist-util-is'
99
/**
1010
* Find the first node in `parent` after another `node` or after an index,
1111
* that passes `test`.
12-
13-
* @param parent
12+
*
13+
* @template {Node} Kind
14+
* Node type.
15+
*
16+
* @overload
17+
* @param {Parent} parent
18+
* @param {Node | number} index
19+
* @param {import('unist-util-is').PredicateTest<Kind>} test
20+
* @returns {Kind | null}
21+
*
22+
* @overload
23+
* @param {Parent} parent
24+
* @param {Node | number} index
25+
* @param {Test} [test]
26+
* @returns {Node | null}
27+
*
28+
* @param {Parent} parent
1429
* Parent node.
15-
* @param index
30+
* @param {Node | number} index
1631
* Child of `parent` or it’s index.
17-
* @param test
32+
* @param {Test} [test]
1833
* `unist-util-is`-compatible test.
19-
* @returns
34+
* @returns {Node | null}
2035
* Child of `parent` or `null`.
2136
*/
22-
export const findAfter =
23-
/**
24-
* @type {(
25-
* (<T extends Node>(node: Parent, index: Node | number, test: import('unist-util-is').PredicateTest<T>) => T | null) &
26-
* ((node: Parent, index: Node | number, test?: Test) => Node | null)
27-
* )}
28-
*/
29-
(
30-
/**
31-
* @param {Parent} parent
32-
* @param {Node | number} index
33-
* @param {Test} [test]
34-
* @returns {Node | null}
35-
*/
36-
function (parent, index, test) {
37-
const is = convert(test)
38-
39-
if (!parent || !parent.type || !parent.children) {
40-
throw new Error('Expected parent node')
41-
}
37+
// To do: next major: `undefined`.
38+
export function findAfter(parent, index, test) {
39+
const is = convert(test)
4240

43-
if (typeof index === 'number') {
44-
if (index < 0 || index === Number.POSITIVE_INFINITY) {
45-
throw new Error('Expected positive finite number as index')
46-
}
47-
} else {
48-
index = parent.children.indexOf(index)
41+
if (!parent || !parent.type || !parent.children) {
42+
throw new Error('Expected parent node')
43+
}
4944

50-
if (index < 0) {
51-
throw new Error('Expected child node or index')
52-
}
53-
}
45+
if (typeof index === 'number') {
46+
if (index < 0 || index === Number.POSITIVE_INFINITY) {
47+
throw new Error('Expected positive finite number as index')
48+
}
49+
} else {
50+
index = parent.children.indexOf(index)
5451

55-
while (++index < parent.children.length) {
56-
if (is(parent.children[index], index, parent)) {
57-
return parent.children[index]
58-
}
59-
}
52+
if (index < 0) {
53+
throw new Error('Expected child node or index')
54+
}
55+
}
6056

61-
return null
57+
while (++index < parent.children.length) {
58+
if (is(parent.children[index], index, parent)) {
59+
return parent.children[index]
6260
}
63-
)
61+
}
62+
63+
return null
64+
}

0 commit comments

Comments
 (0)