unist utility to create a new tree with all nodes that pass the given test.
npm:
npm install unist-util-filter
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' }
]
}
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.
tree
(Node?
) — Tree to filteroptions.cascade
(boolean
, default:true
) — Whether to drop parent nodes if they had children, but all their children were filtered outtest
(Test
, optional) —is
-compatible test (such as a type)
Node?
— New filtered tree.
null
is returned if tree
itself didn’t pass the test, or is cascaded away.
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.
MIT © Eugene Sharygin