unist-util-filter

unist utility to create a new tree with nodes that pass a filter

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
unist-util-filter
904.0.0a year ago7 years agoMinified + gzip package size for unist-util-filter in KB

Readme

unist-util-filter
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
unistunist utility to create a new tree with all nodes that pass the given test.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.
npm install unist-util-filter
## Use

```js
import {u} from 'unist-builder'
import {filter} from 'unist-util-filter'

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

const 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

This package exports the following identifiers: filter. There is no default export.

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
— [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 (Testis, optional) — isis-compatible test (such as a
[type][])
Returns
Node?node — New filtered tree. null is returned if tree itself didn’t pass the test, or is cascaded away.

Related

— Recursively walk over nodes
— Like `visit`, but with a stack of parents
— Create a new tree with all nodes mapped by a given function
— Create a new tree by mapping (to an array) by a given function
— Remove nodes from a tree that pass a test
— Select nodes with CSS-like selectors

Contribute

See contributing.md in syntax-tree/.githubcontributing for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MITlicense © Eugene Sharygin