Skip to content

Commit 37a404c

Browse files
committed
Added README
1 parent 9ab151e commit 37a404c

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"repository": {
2424
"type": "git",
25-
"url": "git+https://github.com/mrzmmr/unist-util-find-all-between.git"
25+
"url": "git+https://github.com/unicorn-utterances/unist-util-replace-all-between.git"
2626
},
2727
"keywords": [
2828
"unist",
@@ -32,12 +32,11 @@
3232
"util",
3333
"utility"
3434
],
35-
"author": "mrzmmr",
3635
"license": "MIT",
3736
"bugs": {
38-
"url": "https://github.com/mrzmmr/unist-util-find-all-between/issues"
37+
"url": "https://github.com/unicorn-utterances/unist-util-replace-all-between/issues"
3938
},
40-
"homepage": "https://github.com/mrzmmr/unist-util-find-all-between#readme",
39+
"homepage": "https://github.com/unicorn-utterances/unist-util-replace-all-between#readme",
4140
"dependencies": {
4241
"unist-util-is": "^5.1.1"
4342
},

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export default function findAllBetween<T extends Node>(
1414
parent: Parent,
1515
start: Node,
1616
end: Node,
17-
func: (node: Node[]) => Node[]
17+
func: (nodes: Node[]) => Node[]
1818
): Node[][];

0 commit comments

Comments
 (0)