unist-util-remove
unist utility to remove nodes from a tree
Last updated 7 months ago by wooorm .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install unist-util-remove 
SYNC missed versions from official npm registry.

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
  • tree (Node?) — Tree to filter
  • options.cascade (boolean, default: true) — Whether to drop parent nodes if they had children, but all their children were filtered out
  • test (Test, optional) — is-compatible test (such as a type)
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.

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

License

MIT © Eugene Sharygin

Current Tags

  • 2.0.0                                ...           latest (7 months ago)

8 Versions

  • 2.0.0                                ...           7 months ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           3 years ago
  • 0.2.1                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
Maintainers (2)
Downloads
Today 373
This Week 778
This Month 8,041
Last Day 405
Last Week 2,439
Last Month 13,013
Dependencies (1)
Dev Dependencies (7)

Copyright 2014 - 2016 © taobao.org |