unified

Project: syntax-tree/unist-util-remove

Package: unist-util-remove@2.0.0

  1. Dependencies: 1·Dependents: 47
  2. unist utility to remove nodes from a tree
  1. util 107
  2. utility 105
  3. unist 97
  4. tree 40
  5. ast 27
  6. unist-util 24
  7. node 21
  8. remove 10
  9. squeeze 3
  10. strip 2

unist-util-remove

Build Coverage Downloads Size Sponsors Backers Chat

unist utility to modify the given tree by removing all nodes that pass the given test.

Install

npm:

npm install unist-util-remove

Use

var u = require('unist-builder')
var remove = require('unist-util-remove')

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

// Remove all nodes of type `leaf`.
remove(tree, 'leaf')

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

Yields: (note the parent of 5 is also removed, due to options.cascade)

{
  type: 'root',
  children: [
    {
      type: 'node',
      children: [
        { type: 'node', children: [ { type: 'other', value: '4' } ] }
      ]
    }
  ]
}

API

remove(tree[, options][, test])

Mutate the given tree by removing all nodes that pass test. The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.

Parameters
Returns

Node? — The given tree with nodes for which test passed removed. null is returned if tree itself didn’t pass the test, or is cascaded away.

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, organisation, or community you agree to abide by its terms.

License

MIT © Eugene Sharygin