unist-util-modify-children
unist utility to modify direct children of a parent
Last updated 5 years ago by wooorm .
MIT · Repository · Bugs · Original npm
$ cnpm install unist-util-modify-children 
SYNC missed versions from official npm registry.

unist-util-modify-children

Build Coverage Downloads Size Sponsors Backers Chat

Modify direct children of a parent.

Install

npm:

npm install unist-util-modify-children

Use

var u = require('unist-builder')
var modifyChildren = require('unist-util-modify-children')

var modify = modifyChildren(modifier)

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

modify(tree)

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
  }
}

Yields:

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

API

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.

Returns

number (optional) — Next position to iterate.

function modify(parent)

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

Related

Contribute

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.

License

MIT © Titus Wormer

Current Tags

  • 2.0.0                                ...           latest (5 years ago)

3 Versions

  • 3.0.0                                ...           4 years ago
  • 2.0.0                                ...           5 years ago
  • 1.1.6                                ...           6 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (10)
Dependents (0)
None

Copyright 2013 - present © cnpmjs.org