A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/unicorn-utterances/unist-util-replace-all-between below:

playfulprogramming/unist-util-replace-all-between: Replace section of nodes in Unist parent using selections for start/end

unist-util-replace-all-between

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

npm:

npm install unist-util-replace-all-between
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' },
]
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

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4