1
- # unist-util-filter [ ![ Build Status ] [ build-badge ]] [ build-page ] [ ![ Coverage Status ] [ coverage-badge ]] [ coverage-page ]
1
+ # unist-util-filter
2
2
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.
4
10
5
11
## Install
6
12
13
+ [ npm] [ ] :
14
+
7
15
``` sh
8
16
npm install unist-util-filter
9
17
```
10
18
11
19
## Usage
12
20
13
21
``` js
14
- var filter = require (' unist-util-filter' );
22
+ var u = require (' unist-builder' )
23
+ var filter = require (' unist-util-filter' )
15
24
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 )
30
32
31
- console .log ( filter (tree, node => node . type != ' leaf ' || node . value % 2 == 0 ) )
33
+ console .dir (newTree, {depth : null } )
32
34
```
33
35
34
36
Yields:
@@ -37,64 +39,87 @@ Yields:
37
39
{
38
40
type: ' root' ,
39
41
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' }
42
44
]
43
45
}
44
46
```
45
47
46
48
## API
47
49
48
- ### ` filter(tree, [opts] , test) `
50
+ ### ` filter(tree[, options][ , test] ) `
49
51
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.
52
55
53
56
###### Parameters
54
57
55
58
* ` 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 ] [ ] )
62
65
63
66
###### Returns
64
67
65
- A new tree ( [ ` Node? ` ] [ node ] ) with nodes for which ` test ` returned ` true ` .
68
+ [ ` Node? ` ] [ node ] — New filtered [ tree ] [ ] .
66
69
` null ` is returned if ` tree ` itself didn’t pass the test, or is cascaded away.
67
70
68
71
## Contribute
69
72
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
71
74
started.
75
+ See [ ` support.md ` ] [ support ] for ways to get help.
72
76
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.
75
80
76
81
## License
77
82
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
79
90
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
81
106
82
107
[ unist ] : https://github.com/syntax-tree/unist
83
108
84
109
[ node ] : https://github.com/syntax-tree/unist#node
85
110
86
- [ is ] : https://github.com/syntax-tree/unist-util-is
111
+ [ tree ] : https://github.com/syntax-tree/unist#tree
87
112
88
- [ preorder ] : https://en.wikipedia .org/wiki/Tree_traversal
113
+ [ preorder ] : https://www.geeksforgeeks .org/tree-traversals-inorder-preorder-and-postorder/
89
114
90
- [ build-page ] : https://travis-ci.org /syntax-tree/unist-util-filter
115
+ [ head ] : https://github.com /syntax-tree/unist#head
91
116
92
- [ build-badge ] : https://travis-ci.org /syntax-tree/unist-util-filter.svg?branch=master
117
+ [ type ] : https://github.com /syntax-tree/unist#type
93
118
94
- [ coverage-page ] : https://codecov.io/github/ syntax-tree/unist-util-filter?branch=master
119
+ [ is ] : https://github.com/ syntax-tree/unist-util-is
95
120
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
97
122
98
- [ contributing ] : https://github.com/syntax-tree/unist /blob/master/contributing .md
123
+ [ support ] : https://github.com/syntax-tree/.github /blob/master/support .md
99
124
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