Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2
utility to create a new tree with nodes that pass a filter
License
syntax-tree/unist-util-filter
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
unist utility to create a new tree with only nodes that pass a test.
This is a small utility that helps you clean a tree.
You can use this utility to remove things from a tree.This utility is very similar tounist-util-remove, whichchanges the given tree.Modifying a tree like that utility does is much faster on larger documents.
You can also walk the tree withunist-util-visit to removenodes.To create trees, useunist-builder.
This package isESM only.In Node.js (version 16+), install withnpm:
npm install unist-util-filter
In Deno withesm.sh:
import{filter}from'https://esm.sh/unist-util-filter@5'
In browsers withesm.sh:
<scripttype="module">import{filter}from'https://esm.sh/unist-util-filter@5?bundle'</script>
import{u}from'unist-builder'import{filter}from'unist-util-filter'consttree=u('root',[u('leaf','1'),u('parent',[u('leaf','2'),u('parent',[u('leaf','3')])]),u('leaf','4')])constnewTree=filter(tree,node=>node.type!=='leaf'||node.value%2===0)console.dir(newTree,{depth:null})
Yields:
{type:'root',children:[{type:'parent',children:[{type:'leaf',value:'2'}]},{type:'leaf',value:'4'}]}
This package exports the identifierfilter.There is no default export.
Create a newtree of copies of all nodes that passtest.
The tree is walked inpreorder (NLR), visiting the node itself, then itshead, etc.
tree(Node)— tree to filteroptions(Options, optional)— configurationtest(Test, optional)—unist-util-iscompatible test
New filtered tree (Node orundefined).
undefined is returned iftree itself didn’t pass the test, or is cascadedaway.
Configuration (TypeScript type).
cascade(boolean, default:true)— whether to drop parent nodes if they had children, but all theirchildren were filtered out
This package is fully typed withTypeScript.It exports the additional typeOptions.
Projects maintained by the unified collective are compatible with maintainedversions of Node.js.
When we cut a new major release, we drop support for unmaintained versions ofNode.This means we try to keep the current release line,unist-util-filter@^5,compatible with Node.js 16.
unist-util-visit— walk the treeunist-util-visit-parents— walk the tree with a stack of parentsunist-util-map— create a new tree with all nodes mapped by a given functionunist-util-flatmap— create a new tree by mapping (to an array) by a given functionunist-util-remove— remove nodes from a tree that pass a testunist-util-select— select nodes with CSS-like selectors
Seecontributing.md insyntax-tree/.github forways to get started.Seesupport.md for ways to get help.
This project has acode of conduct.By interacting with this repository, organization, or community you agree toabide by its terms.
MIT © Eugene Sharygin
About
utility to create a new tree with nodes that pass a filter
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.