unist-util-replace-all-between
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

unist-util-replace-all-between

unist utility to modify an existing child list to replace all elements between all instances of two nodes

Install

npm:

npm install unist-util-replace-all-between

Usage

import u from 'unist-builder'
import replaceAllBetween from 'unist-util-replace-all-between'

const tree = u('root', [
  u('start', '1'),
  u('node', [u('leaf', '2'), u('node', [u('leaf', '3')])]),
  u('end', '4'),
  u('middle', '1'),
  u('start', '1'),
  u('node', '2'),
  u('end', '4'),
])

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

console.dir(newChildren, {depth: null})

Yields:

[
    { type: 'replaced', value: '1' },
    { type: 'middle', value: '2' },
    { type: 'replaced', value: '1' },
]

API

replaceAllBetween(parent, start, end, func)

Mutate an existing parent's children to reflect function return

Parent's children are only search. None of their children (or further down) are searched

Parameters
  • parent (Parent) — Parent to walk through children of
  • start (Test) — is-compatible test (such as a type) to find the start of each section
  • end (Test) — is-compatible test (such as a type) to find the end of each section
  • func ((nodes: Node[]) => Node[]) — Function used to change nodes. Return value is then set to the parent.children value
Returns

Node[] — List of children from parent post-mutation

Package Sidebar

Install

npm i unist-util-replace-all-between

Weekly Downloads

15

Version

1.0.0

License

MIT

Unpacked Size

6.7 kB

Total Files

4

Last publish

Collaborators

  • crutchcorn