|
1 | | -/** |
2 | | - * @author Titus Wormer |
3 | | - * @copyright 2015 Titus Wormer |
4 | | - * @license MIT |
5 | | - * @module unist:util:find-after |
6 | | - * @fileoverview Utility to find a node after another node. |
7 | | - */ |
8 | | - |
9 | 1 | 'use strict'; |
10 | 2 |
|
11 | | -/* eslint-env commonjs */ |
12 | | - |
13 | | -/* |
14 | | - * Dependencies. |
15 | | - */ |
16 | | - |
17 | 3 | var is = require('unist-util-is'); |
18 | 4 |
|
19 | | -/** |
20 | | - * Find a node after `index` in `parent` which passes |
21 | | - * `test`. |
22 | | - * |
23 | | - * @param {Node} parent - Parent to search in. |
24 | | - * @param {number|Node} index - (Position of) node to |
25 | | - * search after. |
26 | | - * @param {*} test - See `wooorm/unist-util-is`. |
27 | | - * @return {Node?} - A child node of `parent` which passes |
28 | | - * `test`. |
29 | | - */ |
| 5 | +module.exports = findAfter; |
| 6 | + |
| 7 | +/* Find a node after `index` in `parent` which passes |
| 8 | + * `test`. */ |
30 | 9 | function findAfter(parent, index, test) { |
31 | | - var children; |
32 | | - var child; |
33 | | - var length; |
| 10 | + var children; |
| 11 | + var child; |
| 12 | + var length; |
34 | 13 |
|
35 | | - if (!parent || !parent.type || !parent.children) { |
36 | | - throw new Error('Expected parent node'); |
37 | | - } |
| 14 | + if (!parent || !parent.type || !parent.children) { |
| 15 | + throw new Error('Expected parent node'); |
| 16 | + } |
38 | 17 |
|
39 | | - children = parent.children; |
40 | | - length = children.length; |
| 18 | + children = parent.children; |
| 19 | + length = children.length; |
41 | 20 |
|
42 | | - if (index && index.type) { |
43 | | - index = children.indexOf(index); |
44 | | - } |
| 21 | + if (index && index.type) { |
| 22 | + index = children.indexOf(index); |
| 23 | + } |
45 | 24 |
|
46 | | - if (isNaN(index) || index < 0 || index === Infinity) { |
47 | | - throw new Error('Expected positive finite index or child node'); |
48 | | - } |
| 25 | + if (isNaN(index) || index < 0 || index === Infinity) { |
| 26 | + throw new Error('Expected positive finite index or child node'); |
| 27 | + } |
49 | 28 |
|
50 | | - while (++index < length) { |
51 | | - child = children[index]; |
| 29 | + while (++index < length) { |
| 30 | + child = children[index]; |
52 | 31 |
|
53 | | - if (is(test, child, index, parent)) { |
54 | | - return child; |
55 | | - } |
| 32 | + if (is(test, child, index, parent)) { |
| 33 | + return child; |
56 | 34 | } |
| 35 | + } |
57 | 36 |
|
58 | | - return null; |
| 37 | + return null; |
59 | 38 | } |
60 | | - |
61 | | -/* |
62 | | - * Expose. |
63 | | - */ |
64 | | - |
65 | | -module.exports = findAfter; |
0 commit comments