|
| 1 | +# unist-util-find-all-between |
| 2 | + |
| 3 | +[**unist**](https://github.com/syntax-tree/unist) utility to modify an existing child list to replace all elements between all |
| 4 | +instances of two nodes |
| 5 | + |
| 6 | +## Install |
| 7 | + |
| 8 | +[npm](https://docs.npmjs.com/cli/install): |
| 9 | + |
| 10 | +```sh |
| 11 | +npm install unist-util-find-all-between |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```js |
| 17 | +import u from 'unist-builder' |
| 18 | +import findAllBetween from 'unist-util-find-all-between' |
| 19 | + |
| 20 | +const tree = u('root', [ |
| 21 | + u('start', '1'), |
| 22 | + u('node', [u('leaf', '2'), u('node', [u('leaf', '3')])]), |
| 23 | + u('end', '4'), |
| 24 | + u('middle', '1'), |
| 25 | + u('start', '1'), |
| 26 | + u('node', '2'), |
| 27 | + u('end', '4'), |
| 28 | +]) |
| 29 | + |
| 30 | +const newChildren = findAllBetween(tree, {type: 'start'}, {type: 'end'}, () => [u('replaced', '1')]) |
| 31 | + |
| 32 | +console.dir(newChildren, {depth: null}) |
| 33 | +``` |
| 34 | + |
| 35 | +Yields: |
| 36 | + |
| 37 | +```js |
| 38 | +[ |
| 39 | + { type: 'replaced', value: '1' }, |
| 40 | + { type: 'middle', value: '2' }, |
| 41 | + { type: 'replaced', value: '1' }, |
| 42 | +] |
| 43 | +``` |
| 44 | + |
| 45 | +## API |
| 46 | + |
| 47 | +### `findAllBetween(parent, start, end, func)` |
| 48 | + |
| 49 | +Mutate an existing parent's children to reflect function return |
| 50 | + |
| 51 | +Parent's children are only search. None of their children (or further down) are searched |
| 52 | + |
| 53 | +###### Parameters |
| 54 | + |
| 55 | +* `parent` ([`Parent`](https://github.com/syntax-tree/unist#parent)) |
| 56 | + — Parent to walk through children of |
| 57 | +* `start` ([`Test`](https://github.com/syntax-tree/unist-util-is)) — [`is`](https://github.com/syntax-tree/unist-util-is)-compatible test (such as a |
| 58 | + [type](https://github.com/syntax-tree/unist#type)) to find the start of each section |
| 59 | +* `end` ([`Test`](https://github.com/syntax-tree/unist-util-is)) — [`is`](https://github.com/syntax-tree/unist-util-is)-compatible test (such as a |
| 60 | + [type](https://github.com/syntax-tree/unist#type)) to find the end of each section |
| 61 | +* `func` ((`nodes`: [`Node[]`](https://github.com/syntax-tree/unist#node)) `=>` [`Node[]`](https://github.com/syntax-tree/unist#node)) — Function |
| 62 | + used to change nodes. Return value is then set to the parent.children value |
| 63 | + |
| 64 | +###### Returns |
| 65 | + |
| 66 | +[`Node[]`](https://github.com/syntax-tree/unist#node) — List of children from `parent` post-mutation |
0 commit comments