Skip to content

Commit

Permalink
fix: don't throw error when nothing replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Jan 10, 2022
1 parent 37a404c commit 39f7a09
Show file tree
Hide file tree
Showing 4 changed files with 9,652 additions and 12 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# unist-util-find-all-between
# unist-util-replace-all-between

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

```sh
npm install unist-util-find-all-between
npm install unist-util-replace-all-between
```

## Usage

```js
import u from 'unist-builder'
import findAllBetween from 'unist-util-find-all-between'
import replaceAllBetween from 'unist-util-replace-all-between'

const tree = u('root', [
u('start', '1'),
Expand All @@ -27,7 +27,7 @@ const tree = u('root', [
u('end', '4'),
])

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

console.dir(newChildren, {depth: null})
```
Expand All @@ -44,7 +44,7 @@ Yields:

## API

### `findAllBetween(parent, start, end, func)`
### `replaceAllBetween(parent, start, end, func)`

Mutate an existing parent's children to reflect function return

Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ function replaceAllBetween(parent, start, end, func) {
return previous;
}, []);

// Nothing found to replace
if (ranges.length === 0) {
return children;
}

if (!ranges[ranges.length - 1].end) {
console.error('No ending value was found');
throw new Error('No ending value was found');
Expand Down
Loading

0 comments on commit 39f7a09

Please sign in to comment.