@@ -9,61 +9,61 @@ import {convert} from 'unist-util-is'
99/**
1010 * Find the first node in `parent` before another `node` or before 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`.
21- *
2236 */
23- export const findBefore =
24- /**
25- * @type {(
26- * (<T extends Node>(node: Parent, index: Node | number, test: import('unist-util-is').PredicateTest<T>) => T | null) &
27- * ((node: Parent, index: Node | number, test?: Test) => Node | null)
28- * )}
29- */
30- (
31- /**
32- * @param {Parent } parent
33- * @param {Node | number } index
34- * @param {Test } [test]
35- * @returns {Node | null }
36- */
37- function ( parent , index , test ) {
38- const is = convert ( test )
39-
40- if ( ! parent || ! parent . type || ! parent . children ) {
41- throw new Error ( 'Expected parent node' )
42- }
37+ // To do: next major: return undefined.
38+ export function findBefore ( parent , index , test ) {
39+ const is = convert ( test )
4340
44- if ( typeof index === 'number' ) {
45- if ( index < 0 || index === Number . POSITIVE_INFINITY ) {
46- throw new Error ( 'Expected positive finite number as index' )
47- }
48- } else {
49- index = parent . children . indexOf ( index )
41+ if ( ! parent || ! parent . type || ! parent . children ) {
42+ throw new Error ( 'Expected parent node' )
43+ }
5044
51- if ( index < 0 ) {
52- throw new Error ( 'Expected child node or index' )
53- }
54- }
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 )
5551
56- // Performance.
57- if ( index > parent . children . length ) {
58- index = parent . children . length
59- }
52+ if ( index < 0 ) {
53+ throw new Error ( 'Expected child node or index' )
54+ }
55+ }
6056
61- while ( index -- ) {
62- if ( is ( parent . children [ index ] , index , parent ) ) {
63- return parent . children [ index ]
64- }
65- }
57+ // Performance.
58+ if ( index > parent . children . length ) {
59+ index = parent . children . length
60+ }
6661
67- return null
62+ while ( index -- ) {
63+ if ( is ( parent . children [ index ] , index , parent ) ) {
64+ return parent . children [ index ]
6865 }
69- )
66+ }
67+
68+ return null
69+ }
0 commit comments