unified

Project: syntax-tree/unist-util-filter

Package: unist-util-filter@2.0.2

  1. Dependencies: 2·Dependents: 30
  2. unist utility to create a new tree with nodes that pass a filter
  1. util 107
  2. utility 105
  3. unist 97
  4. tree 40
  5. ast 27
  6. unist-util 24
  7. node 21
  8. test 6
  9. filter 4

unist-util-filter

Build Coverage Downloads Size Sponsors Backers Chat

unist utility to create a new tree with all nodes that pass the given test.

Install

npm:

npm install unist-util-filter

Use

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

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

var newTree = filter(tree, node => node.type !== 'leaf' || node.value % 2 === 0)

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

Yields:

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

API

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

Create a new tree consisting of copies of all nodes that pass test. The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.

Parameters
Returns

Node? — New filtered tree. 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, organization, or community you agree to abide by its terms.

License

MIT © Eugene Sharygin