Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

syntax-tree/unist-util-filter

BuildCoverageDownloadsSizeSponsorsBackersChat

unist utility to create a new tree with only nodes that pass a test.

Contents

What is this?

This is a small utility that helps you clean a tree.

When should I use this?

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.

Install

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>

Use

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'}]}

API

This package exports the identifierfilter.There is no default export.

filter(tree[, options][, test])

Create a newtree of copies of all nodes that passtest.

The tree is walked inpreorder (NLR), visiting the node itself, then itshead, etc.

Parameters
  • tree (Node)— tree to filter
  • options (Options, optional)— configuration
  • test (Test, optional)—unist-util-is compatible test
Returns

New filtered tree (Node orundefined).

undefined is returned iftree itself didn’t pass the test, or is cascadedaway.

Options

Configuration (TypeScript type).

Fields
  • cascade (boolean, default:true)— whether to drop parent nodes if they had children, but all theirchildren were filtered out

Types

This package is fully typed withTypeScript.It exports the additional typeOptions.

Compatibility

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.

Related

Contribute

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.

License

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

Stars

Watchers

Forks

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp