|
| 1 | +/** |
| 2 | + * @typedef {import('unist').Node} Node |
| 3 | + * @typedef {import('unist').Parent} Parent |
| 4 | + * |
| 5 | + * @typedef {import('unist-util-is').Type} Type |
| 6 | + * @typedef {import('unist-util-is').Props} Props |
| 7 | + * @typedef {import('unist-util-is').TestFunctionAnything} TestFunctionAnything |
| 8 | + */ |
| 9 | + |
1 | 10 | import {convert} from 'unist-util-is' |
2 | 11 |
|
3 | | -export function findBefore(parent, index, test) { |
4 | | - var is = convert(test) |
| 12 | +export var findBefore = |
| 13 | + /** |
| 14 | + * @type {( |
| 15 | + * (<T extends Node>(node: Parent, index: Node|number, test: T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>|Array.<T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>>) => T|null) & |
| 16 | + * ((node: Parent, index: Node|number, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Node|null) |
| 17 | + * )} |
| 18 | + */ |
| 19 | + ( |
| 20 | + /** |
| 21 | + * @param {Parent} parent Parent node |
| 22 | + * @param {Node|number} index Child of `parent`, or it’s index |
| 23 | + * @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test] is-compatible test (such as a type) |
| 24 | + * @returns {Node|null} |
| 25 | + */ |
| 26 | + function (parent, index, test) { |
| 27 | + var is = convert(test) |
5 | 28 |
|
6 | | - if (!parent || !parent.type || !parent.children) { |
7 | | - throw new Error('Expected parent node') |
8 | | - } |
| 29 | + if (!parent || !parent.type || !parent.children) { |
| 30 | + throw new Error('Expected parent node') |
| 31 | + } |
9 | 32 |
|
10 | | - if (typeof index === 'number') { |
11 | | - if (index < 0 || index === Number.POSITIVE_INFINITY) { |
12 | | - throw new Error('Expected positive finite number as index') |
13 | | - } |
14 | | - } else { |
15 | | - index = parent.children.indexOf(index) |
| 33 | + if (typeof index === 'number') { |
| 34 | + if (index < 0 || index === Number.POSITIVE_INFINITY) { |
| 35 | + throw new Error('Expected positive finite number as index') |
| 36 | + } |
| 37 | + } else { |
| 38 | + index = parent.children.indexOf(index) |
16 | 39 |
|
17 | | - if (index < 0) { |
18 | | - throw new Error('Expected child node or index') |
19 | | - } |
20 | | - } |
| 40 | + if (index < 0) { |
| 41 | + throw new Error('Expected child node or index') |
| 42 | + } |
| 43 | + } |
21 | 44 |
|
22 | | - // Performance. |
23 | | - if (index > parent.children.length) { |
24 | | - index = parent.children.length |
25 | | - } |
| 45 | + // Performance. |
| 46 | + if (index > parent.children.length) { |
| 47 | + index = parent.children.length |
| 48 | + } |
26 | 49 |
|
27 | | - while (index--) { |
28 | | - if (is(parent.children[index], index, parent)) { |
29 | | - return parent.children[index] |
30 | | - } |
31 | | - } |
| 50 | + while (index--) { |
| 51 | + if (is(parent.children[index], index, parent)) { |
| 52 | + return parent.children[index] |
| 53 | + } |
| 54 | + } |
32 | 55 |
|
33 | | - return null |
34 | | -} |
| 56 | + return null |
| 57 | + } |
| 58 | + ) |
0 commit comments