Skip to content

Commit 39f7a09

Browse files
committed
fix: don't throw error when nothing replaced
1 parent 37a404c commit 39f7a09

File tree

4 files changed

+9652
-12
lines changed

4 files changed

+9652
-12
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# unist-util-find-all-between
1+
# unist-util-replace-all-between
22

33
[**unist**](https://github.com/syntax-tree/unist) utility to modify an existing child list to replace all elements between all
44
instances of two nodes
@@ -8,14 +8,14 @@ instances of two nodes
88
[npm](https://docs.npmjs.com/cli/install):
99

1010
```sh
11-
npm install unist-util-find-all-between
11+
npm install unist-util-replace-all-between
1212
```
1313

1414
## Usage
1515

1616
```js
1717
import u from 'unist-builder'
18-
import findAllBetween from 'unist-util-find-all-between'
18+
import replaceAllBetween from 'unist-util-replace-all-between'
1919

2020
const tree = u('root', [
2121
u('start', '1'),
@@ -27,7 +27,7 @@ const tree = u('root', [
2727
u('end', '4'),
2828
])
2929

30-
const newChildren = findAllBetween(tree, {type: 'start'}, {type: 'end'}, () => [u('replaced', '1')])
30+
const newChildren = replaceAllBetween(tree, {type: 'start'}, {type: 'end'}, () => [u('replaced', '1')])
3131

3232
console.dir(newChildren, {depth: null})
3333
```
@@ -44,7 +44,7 @@ Yields:
4444

4545
## API
4646

47-
### `findAllBetween(parent, start, end, func)`
47+
### `replaceAllBetween(parent, start, end, func)`
4848

4949
Mutate an existing parent's children to reflect function return
5050

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ function replaceAllBetween(parent, start, end, func) {
4949
return previous;
5050
}, []);
5151

52+
// Nothing found to replace
53+
if (ranges.length === 0) {
54+
return children;
55+
}
56+
5257
if (!ranges[ranges.length - 1].end) {
5358
console.error('No ending value was found');
5459
throw new Error('No ending value was found');

0 commit comments

Comments
 (0)