unist utility to remove nodes from a tree

Downloads in past


1503.1.0a year ago7 years agoMinified + gzip package size for unist-util-remove in KB


!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
unistunist utility to modify the given tree by removing all nodes that pass the given test.


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

import {u} from 'unist-builder'
import {remove} from 'unist-util-remove'

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

// Remove all nodes of type `leaf`.
remove(tree, 'leaf')

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

Yields: (note the parent of 5 is also removed, due to options.cascade)
  type: 'root',
  children: [
      type: 'node',
      children: [{type: 'node', children: [{type: 'other', value: '4'}]}]


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

remove(tree[, options], test)

Mutate the given tree by removing all nodes that pass test. The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.
— [Tree][] to filter
  • options.cascade (boolean, default: true)
— Whether to drop parent nodes if they had children, but all their children
were filtered out
Node?node — The given tree with nodes for which test passed removed. null is returned if tree itself didn’t pass the test, or is cascaded away.


— Create a new tree with all nodes that pass the given function
— Create a new tree by expanding a node into many
— Create a new tree by mapping nodes
— Select nodes with CSS-like selectors
— Recursively walk over nodes
— Creating trees


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, organisation, or community you agree to abide by its terms.


MITlicense © Eugene Sharygin