🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

unist-util-filter

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unist-util-filter

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

5.0.1
latest
Version published
Weekly downloads
516K
1.35%
Maintainers
2
Weekly downloads
 
Created

What is unist-util-filter?

The `unist-util-filter` package is a utility for filtering nodes in a Unist syntax tree. It allows you to traverse and selectively remove nodes based on a predicate function, making it useful for tasks such as cleaning up or transforming abstract syntax trees (ASTs).

What are unist-util-filter's main functionalities?

Filter nodes based on a predicate

This feature allows you to filter nodes in a Unist tree based on a predicate function. In this example, the tree is filtered to remove paragraphs containing the text 'World'.

const filter = require('unist-util-filter');
const tree = {
  type: 'root',
  children: [
    { type: 'paragraph', children: [{ type: 'text', value: 'Hello' }] },
    { type: 'paragraph', children: [{ type: 'text', value: 'World' }] }
  ]
};
const filteredTree = filter(tree, node => node.type !== 'paragraph' || node.children[0].value !== 'World');
console.log(filteredTree);

Filter nodes with a specific type

This feature allows you to filter out nodes of a specific type. In this example, all 'heading' nodes are removed from the tree.

const filter = require('unist-util-filter');
const tree = {
  type: 'root',
  children: [
    { type: 'paragraph', children: [{ type: 'text', value: 'Hello' }] },
    { type: 'heading', children: [{ type: 'text', value: 'Title' }] }
  ]
};
const filteredTree = filter(tree, node => node.type !== 'heading');
console.log(filteredTree);

Other packages similar to unist-util-filter

FAQs

Package last updated on 26 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts