Project: syntax-tree/unist-util-modify-children

Package: unist-util-modify-children@3.0.0

  1. Dependents: 23
  2. unist utility to modify direct children of a parent
  1. util 144
  2. utility 140
  3. unist 127
  4. tree 41
  5. ast 33
  6. unist-util 25
  7. children 2


Build Coverage Downloads Size Sponsors Backers Chat

Modify direct children of a parent.


This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.


npm install unist-util-modify-children


import u from 'unist-builder'
import {modifyChildren} from 'unist-util-modify-children'

const modify = modifyChildren(modifier)

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


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

function modifier(node, index, parent) {
  if (node.type === 'node') {
    parent.children.splice(index, 1, {type: 'subtree', children: node.children})
    return index + 1


  type: 'root',
  children: [
    {type: 'leaf', value: '1'},
    {type: 'subtree', children: [{type: 'leaf', value: '2'}]},
    {type: 'leaf', value: '3'}


This package exports the following identifiers: modifyChildren. There is no default export.

modify = modifyChildren(modifier)

Wrap modifier to be invoked for each child in the node given to modify.

next? = modifier(child, index, parent)

Invoked if modify is called on a parent node for each child in parent.


number (optional) — Next position to iterate.

function modify(parent)

Invoke the bound modifier for each child in parent (Node).


See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.


MIT © Titus Wormer