Skip to content

Create a new Unist tree by filtering with the provided function and getting all of the returned results in a flat list, not including parent nodes

Notifications You must be signed in to change notification settings

playfulprogramming/unist-util-flat-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

unist-util-filter

unist utility to create a new tree with all nodes that pass the given test.

Install

npm:

npm install unist-util-flat-filter

Usage

import u from 'unist-builder';
import flatFilter from 'unist-util-flat-filter';

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

const newTree = flatFilter(tree, node => node.type === 'leaf')

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

Yields:

{
  type: 'root',
  children: [
    { type: 'leaf', value: '1' },
    { type: 'leaf', value: '2' },
    { type: 'leaf', value: '3' },
    { type: 'leaf', value: '4' }
  ]
}

API

filter(tree[, test])

Create a new tree consisting of copies of all nodes that pass test. The tree is walked in inorder, visiting the parent, then the children nodes node itself, etc.

Parameters
  • tree (Node?) — Tree to filter
  • 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.

About

Create a new Unist tree by filtering with the provided function and getting all of the returned results in a flat list, not including parent nodes

Resources

Stars

Watchers

Forks

Packages

No packages published