unist-util-filter
unist utility to create a new tree with nodes that pass a filter
Last updated 5 years ago by wooorm .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install unist-util-filter 
SYNC missed versions from official npm registry.

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

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install unist-util-filter

Use

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

License

MIT © Eugene Sharygin

Current Tags

  • 4.0.0                                ...           latest (5 years ago)

12 Versions

  • 4.0.0                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.3                                ...           5 years ago
  • 2.0.2                                ...           6 years ago
  • 2.0.1                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.0.2                                ...           7 years ago
  • 1.0.1                                ...           7 years ago
  • 1.0.0                                ...           8 years ago
  • 0.2.1                                ...           10 years ago
  • 0.2.0                                ...           10 years ago
  • 0.1.0                                ...           10 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (13)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |