8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** unist** ] [ unist ] utility to create a new tree with all nodes that pass the
12
- given test.
11
+ [ unist] [ ] utility to create a new tree with only nodes that pass a test.
13
12
14
- ## Install
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When should I use this?] ( #when-should-i-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` filter(tree[, options][, test]) ` ] ( #filtertree-options-test )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Related] ( #related )
24
+ * [ Contribute] ( #contribute )
25
+ * [ License] ( #license )
26
+
27
+ ## What is this?
28
+
29
+ This is a small utility that helps you clean a tree.
30
+
31
+ ## When should I use this?
15
32
16
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
17
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
33
+ You can use this utility to remove things from a tree.
34
+ This utility is very similar to [ ` unist-util-remove ` ] [ unist-util-remove ] , which
35
+ changes the given tree.
36
+ Modifying a tree like that utility does is much faster on larger documents.
18
37
19
- [ npm] [ ] :
38
+ You can also walk the tree with [ ` unist-util-visit ` ] [ unist-util-visit ] to remove
39
+ nodes.
40
+ To create trees, use [ ` unist-builder ` ] [ unist-builder ] .
41
+
42
+ ## Install
43
+
44
+ This package is [ ESM only] [ esm ] .
45
+ In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [ npm] [ ] :
20
46
21
47
``` sh
22
48
npm install unist-util-filter
23
49
```
24
50
51
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
52
+
53
+ ``` js
54
+ import {filter } from " https://esm.sh/unist-util-filter@4"
55
+ ```
56
+
57
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
58
+
59
+ ``` html
60
+ <script type =" module" >
61
+ import {filter } from " https://esm.sh/unist-util-filter@4?bundle"
62
+ </script >
63
+ ```
64
+
25
65
## Use
26
66
27
67
``` js
@@ -30,7 +70,7 @@ import {filter} from 'unist-util-filter'
30
70
31
71
const tree = u (' root' , [
32
72
u (' leaf' , ' 1' ),
33
- u (' node ' , [u (' leaf' , ' 2' ), u (' node ' , [u (' leaf' , ' 3' )])]),
73
+ u (' parent ' , [u (' leaf' , ' 2' ), u (' parent ' , [u (' leaf' , ' 3' )])]),
34
74
u (' leaf' , ' 4' )
35
75
])
36
76
@@ -45,57 +85,69 @@ Yields:
45
85
{
46
86
type: ' root' ,
47
87
children: [
48
- {type: ' node ' , children: [{type: ' leaf' , value: ' 2' }]},
88
+ {type: ' parent ' , children: [{type: ' leaf' , value: ' 2' }]},
49
89
{type: ' leaf' , value: ' 4' }
50
90
]
51
91
}
52
92
```
53
93
54
94
## API
55
95
56
- This package exports the following identifiers: ` filter ` .
96
+ This package exports the identifier ` filter ` .
57
97
There is no default export.
58
98
59
99
### ` filter(tree[, options][, test]) `
60
100
61
- Create a new [ tree] [ ] consisting of copies of all nodes that pass ` test ` .
101
+ Create a new ` tree ` ([ ` Node ` ] [ node ] ) of copies of all nodes that pass ` test ` .
102
+ (` Test ` from [ ` unist-util-is ` ] [ test ] ).
62
103
The tree is walked in [ preorder] [ ] (NLR), visiting the node itself, then its
63
- [ head] [ ] , etc.
104
+ head, etc.
64
105
65
- ###### Parameters
106
+ ##### ` options `
66
107
67
- * ` tree ` ([ ` Node? ` ] [ node ] )
68
- — [ Tree] [ ] to filter
69
- * ` options.cascade ` (` boolean ` , default: ` true ` )
70
- — Whether to drop parent nodes if they had children, but all their children
71
- were filtered out
72
- * ` test ` ([ ` Test ` ] [ is ] , optional) — [ ` is ` ] [ is ] -compatible test (such as a
73
- [ type] [ ] )
108
+ Configuration (optional).
109
+
110
+ ###### ` options.cascade `
111
+
112
+ Whether to drop parent nodes if they had children, but all their children were
113
+ filtered out (` boolean ` , default: ` true ` ).
74
114
75
115
###### Returns
76
116
77
- [ ` Node? ` ] [ node ] — New filtered [ tree ] [ ] .
117
+ New filtered tree ( [ ` Node? ` ] [ node ] ) .
78
118
` null ` is returned if ` tree ` itself didn’t pass the test, or is cascaded away.
79
119
120
+ ## Types
121
+
122
+ This package is fully typed with [ TypeScript] [ ] .
123
+ It exports the additional type ` Options ` .
124
+
125
+ ## Compatibility
126
+
127
+ Projects maintained by the unified collective are compatible with all maintained
128
+ versions of Node.js.
129
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
130
+ Our projects sometimes work with older versions, but this is not guaranteed.
131
+
80
132
## Related
81
133
82
134
* [ ` unist-util-visit ` ] ( https://github.com/syntax-tree/unist-util-visit )
83
- — Recursively walk over nodes
135
+ — walk the tree
84
136
* [ ` unist-util-visit-parents ` ] ( https://github.com/syntax-tree/unist-util-visit-parents )
85
- — Like ` visit ` , but with a stack of parents
137
+ — walk the tree with a stack of parents
86
138
* [ ` unist-util-map ` ] ( https://github.com/syntax-tree/unist-util-map )
87
- — Create a new tree with all nodes mapped by a given function
139
+ — create a new tree with all nodes mapped by a given function
88
140
* [ ` unist-util-flatmap ` ] ( https://gitlab.com/staltz/unist-util-flatmap )
89
- — Create a new tree by mapping (to an array) by a given function
141
+ — create a new tree by mapping (to an array) by a given function
90
142
* [ ` unist-util-remove ` ] ( https://github.com/syntax-tree/unist-util-remove )
91
- — Remove nodes from a tree that pass a test
143
+ — remove nodes from a tree that pass a test
92
144
* [ ` unist-util-select ` ] ( https://github.com/syntax-tree/unist-util-select )
93
- — Select nodes with CSS-like selectors
145
+ — select nodes with CSS-like selectors
94
146
95
147
## Contribute
96
148
97
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
98
- started.
149
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
150
+ ways to get started.
99
151
See [ ` support.md ` ] [ support ] for ways to get help.
100
152
101
153
This project has a [ code of conduct] [ coc ] .
@@ -136,24 +188,32 @@ abide by its terms.
136
188
137
189
[ npm ] : https://docs.npmjs.com/cli/install
138
190
191
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
192
+
193
+ [ esmsh ] : https://esm.sh
194
+
195
+ [ typescript ] : https://www.typescriptlang.org
196
+
139
197
[ license ] : license
140
198
141
- [ unist ] : https://github.com/syntax-tree/unist
199
+ [ health ] : https://github.com/syntax-tree/.github
142
200
143
- [ node ] : https://github.com/syntax-tree/unist#node
201
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
202
+
203
+ [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support.md
144
204
145
- [ tree ] : https://github.com/syntax-tree/unist#tree
205
+ [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
146
206
147
- [ preorder ] : https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
207
+ [ unist ] : https://github.com/syntax-tree/unist
148
208
149
- [ head ] : https://github.com/syntax-tree/unist#head
209
+ [ node ] : https://github.com/syntax-tree/unist#node
150
210
151
- [ type ] : https://github.com/syntax- tree/unist#type
211
+ [ preorder ] : https://www.geeksforgeeks.org/ tree-traversals-inorder-preorder-and-postorder/
152
212
153
- [ is ] : https://github.com/syntax-tree/unist-util-is
213
+ [ unist-util-remove ] : https://github.com/syntax-tree/unist-util-remove
154
214
155
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
215
+ [ unist-util-visit ] : https://github.com/syntax-tree/unist-util-visit
156
216
157
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support.md
217
+ [ unist-builder ] : https://github.com/syntax-tree/unist-builder
158
218
159
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
219
+ [ test ] : https://github.com/syntax-tree/unist-util-is#test
0 commit comments