1- # unist-util-filter [ ![ Build Status ] [ build-badge ]] [ build-page ] [ ![ Coverage Status ] [ coverage-badge ]] [ coverage-page ]
1+ # unist-util-filter
22
3- Create a new [ unist] [ ] tree with all nodes that pass the given test.
3+ [ ![ Build] [ build-badge ]] [ build ]
4+ [ ![ Coverage] [ coverage-badge ]] [ coverage ]
5+ [ ![ Downloads] [ downloads-badge ]] [ downloads ]
6+ [ ![ Size] [ size-badge ]] [ size ]
7+
8+ [ ** unist** ] [ unist ] utility to create a new tree with all nodes that pass the
9+ given test.
410
511## Install
612
13+ [ npm] [ ] :
14+
715``` sh
816npm install unist-util-filter
917```
1018
1119## Usage
1220
1321``` js
14- var filter = require (' unist-util-filter' );
22+ var u = require (' unist-builder' )
23+ var filter = require (' unist-util-filter' )
1524
16- var tree = {
17- type: ' root' ,
18- children: [
19- {type: ' leaf' , value: ' 1' },
20- {
21- type: ' node' ,
22- children: [
23- {type: ' leaf' , value: ' 2' },
24- {type: ' node' , children: [{type: ' leaf' , value: ' 3' }]}
25- ]
26- },
27- {type: ' leaf' , value: ' 4' }
28- ]
29- }
25+ var tree = u (' root' , [
26+ u (' leaf' , ' 1' ),
27+ u (' node' , [u (' leaf' , ' 2' ), u (' node' , [u (' leaf' , ' 3' )])]),
28+ u (' leaf' , ' 4' )
29+ ])
30+
31+ var newTree = filter (tree, node => node .type !== ' leaf' || node .value % 2 === 0 )
3032
31- console .log ( filter (tree, node => node . type != ' leaf ' || node . value % 2 == 0 ) )
33+ console .dir (newTree, {depth : null } )
3234```
3335
3436Yields:
@@ -37,64 +39,87 @@ Yields:
3739{
3840 type: ' root' ,
3941 children: [
40- {type: ' node' , children: [{ type: ' leaf' , value: ' 2' }] },
41- {type: ' leaf' , value: ' 4' }
42+ { type: ' node' , children: [ { type: ' leaf' , value: ' 2' } ] },
43+ { type: ' leaf' , value: ' 4' }
4244 ]
4345}
4446```
4547
4648## API
4749
48- ### ` filter(tree, [opts] , test) `
50+ ### ` filter(tree[, options][ , test] ) `
4951
50- Creates a copy of ` tree ` consisting of all nodes that pass ` test ` .
51- The tree is filtered in [ preorder] [ ] .
52+ Create a new [ tree] [ ] consisting of copies of all nodes that pass ` test ` .
53+ The tree is walked in [ preorder] [ ] (NLR), visiting the node itself, then its
54+ [ head] [ ] , etc.
5255
5356###### Parameters
5457
5558* ` tree ` ([ ` Node? ` ] [ node ] )
56- — Tree to filter
57- * ` opts .cascade` (` boolean ` , default: ` true ` )
58- — Whether to drop parent nodes if they had children, but all their
59- children were filtered out
60- * ` test `
61- — See [ ` unist-util-is ` ] [ is ] for details
59+ — [ Tree] [ ] to filter
60+ * ` options .cascade` (` boolean ` , default: ` true ` )
61+ — Whether to drop parent nodes if they had children, but all their children
62+ were filtered out
63+ * ` test ` ( [ ` Test ` ] [ is ] , optional) — [ ` is ` ] [ is ] -compatible test (such as a
64+ [ type ] [ ] )
6265
6366###### Returns
6467
65- A new tree ( [ ` Node? ` ] [ node ] ) with nodes for which ` test ` returned ` true ` .
68+ [ ` Node? ` ] [ node ] — New filtered [ tree ] [ ] .
6669` null ` is returned if ` tree ` itself didn’t pass the test, or is cascaded away.
6770
6871## Contribute
6972
70- See [ ` contributing.md ` in ` syntax-tree/unist ` ] [ contributing ] for ways to get
73+ See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
7174started.
75+ See [ ` support.md ` ] [ support ] for ways to get help.
7276
73- This organisation has a [ Code of Conduct] [ coc ] . By interacting with this
74- repository, organisation, or community you agree to abide by its terms.
77+ This project has a [ Code of Conduct] [ coc ] .
78+ By interacting with this repository, organisation, or community you agree to
79+ abide by its terms.
7580
7681## License
7782
78- [ MIT] [ ] © Eugene Sharygin
83+ [ MIT] [ license ] © Eugene Sharygin
84+
85+ <!-- Definitions -->
86+
87+ [ build-badge ] : https://img.shields.io/travis/syntax-tree/unist-util-find-all-after.svg
88+
89+ [ build ] : https://travis-ci.org/syntax-tree/unist-util-find-all-after
7990
80- [ mit ] : license
91+ [ coverage-badge ] : https://img.shields.io/codecov/c/github/syntax-tree/unist-util-find-all-after.svg
92+
93+ [ coverage ] : https://codecov.io/github/syntax-tree/unist-util-find-all-after
94+
95+ [ downloads-badge ] : https://img.shields.io/npm/dm/unist-util-find-all-after.svg
96+
97+ [ downloads ] : https://www.npmjs.com/package/unist-util-find-all-after
98+
99+ [ size-badge ] : https://img.shields.io/bundlephobia/minzip/unist-util-find-all-after.svg
100+
101+ [ size ] : https://bundlephobia.com/result?p=unist-util-find-all-after
102+
103+ [ npm ] : https://docs.npmjs.com/cli/install
104+
105+ [ license ] : license
81106
82107[ unist ] : https://github.com/syntax-tree/unist
83108
84109[ node ] : https://github.com/syntax-tree/unist#node
85110
86- [ is ] : https://github.com/syntax-tree/unist-util-is
111+ [ tree ] : https://github.com/syntax-tree/unist#tree
87112
88- [ preorder ] : https://en.wikipedia .org/wiki/Tree_traversal
113+ [ preorder ] : https://www.geeksforgeeks .org/tree-traversals-inorder-preorder-and-postorder/
89114
90- [ build-page ] : https://travis-ci.org /syntax-tree/unist-util-filter
115+ [ head ] : https://github.com /syntax-tree/unist#head
91116
92- [ build-badge ] : https://travis-ci.org /syntax-tree/unist-util-filter.svg?branch=master
117+ [ type ] : https://github.com /syntax-tree/unist#type
93118
94- [ coverage-page ] : https://codecov.io/github/ syntax-tree/unist-util-filter?branch=master
119+ [ is ] : https://github.com/ syntax-tree/unist-util-is
95120
96- [ coverage-badge ] : https://img.shields.io/codecov/c/github/ syntax-tree/unist-util-filter.svg?branch= master
121+ [ contributing ] : https://github.com/ syntax-tree/.github/blob/ master/contributing.md
97122
98- [ contributing ] : https://github.com/syntax-tree/unist /blob/master/contributing .md
123+ [ support ] : https://github.com/syntax-tree/.github /blob/master/support .md
99124
100- [ coc ] : https://github.com/syntax-tree/unist /blob/master/code-of-conduct.md
125+ [ coc ] : https://github.com/syntax-tree/.github /blob/master/code-of-conduct.md
0 commit comments