8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** unist** ] [ unist ] utility to modify the given tree by removing all nodes that
12
- pass the given test.
11
+ [ unist] [ ] utility to remove all nodes that pass a test from the tree.
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
+ * [ ` remove(tree[, options], test) ` ] ( #removetree-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 by removing some stuff.
15
30
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.
31
+ ## When should I use this?
32
+
33
+ You can use this utility to remove things from a tree.
34
+ This utility is very similar to [ ` unist-util-filter ` ] [ unist-util-filter ] , which
35
+ creates a new tree.
36
+ Modifying a tree like this utility ` unist-util-remove ` does is much faster on
37
+ larger documents though.
38
+
39
+ You can also walk the tree with [ ` unist-util-visit ` ] [ unist-util-visit ] to remove
40
+ nodes.
41
+ To create trees, use [ ` unist-builder ` ] [ unist-builder ] .
42
+
43
+ ## Install
18
44
19
- [ npm] [ ] :
45
+ This package is [ ESM only] [ esm ] .
46
+ In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [ npm] [ ] :
20
47
21
48
``` sh
22
49
npm install unist-util-remove
23
50
```
24
51
52
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
53
+
54
+ ``` js
55
+ import {remove } from " https://esm.sh/unist-util-remove@3"
56
+ ```
57
+
58
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
59
+
60
+ ``` html
61
+ <script type =" module" >
62
+ import {remove } from " https://esm.sh/unist-util-remove@3?bundle"
63
+ </script >
64
+ ```
65
+
25
66
## Use
26
67
27
68
``` js
@@ -30,10 +71,10 @@ import {remove} from 'unist-util-remove'
30
71
31
72
const tree = u (' root' , [
32
73
u (' leaf' , ' 1' ),
33
- u (' node ' , [
74
+ u (' parent ' , [
34
75
u (' leaf' , ' 2' ),
35
- u (' node ' , [u (' leaf' , ' 3' ), u (' other' , ' 4' )]),
36
- u (' node ' , [u (' leaf' , ' 5' )])
76
+ u (' parent ' , [u (' leaf' , ' 3' ), u (' other' , ' 4' )]),
77
+ u (' parent ' , [u (' leaf' , ' 5' )])
37
78
]),
38
79
u (' leaf' , ' 6' )
39
80
])
@@ -44,64 +85,80 @@ remove(tree, 'leaf')
44
85
console .dir (tree, {depth: null })
45
86
```
46
87
47
- Yields: (note the parent of ` 5 ` is also removed, due to ` options.cascade ` )
88
+ Yields:
48
89
49
90
``` js
50
91
{
51
92
type: ' root' ,
52
93
children: [
53
94
{
54
- type: ' node ' ,
95
+ type: ' parent ' ,
55
96
children: [{type: ' node' , children: [{type: ' other' , value: ' 4' }]}]
56
97
}
57
98
]
58
99
}
59
100
```
60
101
102
+ > 👉 ** Note** : the parent of leaf ` 5 ` is also removed, ` options.cascade ` can
103
+ > change that.
104
+
61
105
## API
62
106
63
- This package exports the following identifiers: ` remove ` .
107
+ This package exports the identifier ` remove ` .
64
108
There is no default export.
65
109
66
110
### ` remove(tree[, options], test) `
67
111
68
- Mutate the given [ tree] [ ] by removing all nodes that pass ` test ` .
112
+ Mutate the given ` tree ` ([ ` Node ` ] [ node ] ) by removing all nodes that pass ` test `
113
+ (` Test ` from [ ` unist-util-is ` ] [ test ] ).
69
114
The tree is walked in [ preorder] [ ] (NLR), visiting the node itself, then its
70
- [ head] [ ] , etc.
115
+ head, etc.
71
116
72
- ###### Parameters
117
+ ##### ` options `
73
118
74
- * ` tree ` ([ ` Node? ` ] [ node ] )
75
- — [ Tree] [ ] to filter
76
- * ` options.cascade ` (` boolean ` , default: ` true ` )
77
- — Whether to drop parent nodes if they had children, but all their children
78
- were filtered out
79
- * ` test ` ([ ` Test ` ] [ is ] ) — [ ` is ` ] [ is ] -compatible test (such as a [ type] [ ] )
119
+ Configuration (optional).
80
120
81
- ###### Returns
121
+ ###### ` options.cascade `
82
122
83
- [ ` Node? ` ] [ node ] — The given ` tree ` with nodes for which ` test ` passed removed.
84
- ` null ` is returned if ` tree ` itself didn’t pass the test, or is cascaded away.
123
+ Whether to drop parent nodes if they had children, but all their children were
124
+ filtered out (` boolean ` , default: ` true ` ).
125
+
126
+ ##### Returns
127
+
128
+ The given ` tree ` without nodes that pass ` test ` ([ ` Node? ` ] [ node ] ).
129
+ ` null ` is returned if ` tree ` itself didn’t pass the test or is cascaded away.
130
+
131
+ ## Types
132
+
133
+ This package is fully typed with [ TypeScript] [ ] .
134
+ It exports no additional types (types for the test are in ` unist-util-is ` ).
135
+
136
+ ## Compatibility
137
+
138
+ Projects maintained by the unified collective are compatible with all maintained
139
+ versions of Node.js.
140
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
141
+ Our projects sometimes work with older versions, but this is not guaranteed.
85
142
86
143
## Related
87
144
88
145
* [ ` unist-util-filter ` ] ( https://github.com/syntax-tree/unist-util-filter )
89
- — Create a new tree with all nodes that pass the given function
146
+ — create a new tree with all nodes that pass the given function
90
147
* [ ` unist-util-flatmap ` ] ( https://gitlab.com/staltz/unist-util-flatmap )
91
- — Create a new tree by expanding a node into many
148
+ — create a new tree by expanding a node into many
92
149
* [ ` unist-util-map ` ] ( https://github.com/syntax-tree/unist-util-map )
93
- — Create a new tree by mapping nodes
150
+ — create a new tree by mapping nodes
94
151
* [ ` unist-util-select ` ] ( https://github.com/syntax-tree/unist-util-select )
95
- — Select nodes with CSS-like selectors
152
+ — select nodes with CSS-like selectors
96
153
* [ ` unist-util-visit ` ] ( https://github.com/syntax-tree/unist-util-visit )
97
- — Recursively walk over nodes
154
+ — walk the tree
98
155
* [ ` unist-builder ` ] ( https://github.com/syntax-tree/unist-builder )
99
- — Creating trees
156
+ — create trees
100
157
101
158
## Contribute
102
159
103
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
104
- started.
160
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
161
+ ways to get started.
105
162
See [ ` support.md ` ] [ support ] for ways to get help.
106
163
107
164
This project has a [ Code of Conduct] [ coc ] .
@@ -142,24 +199,32 @@ abide by its terms.
142
199
143
200
[ npm ] : https://docs.npmjs.com/cli/install
144
201
202
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
203
+
204
+ [ esmsh ] : https://esm.sh
205
+
206
+ [ typescript ] : https://www.typescriptlang.org
207
+
145
208
[ license ] : license
146
209
147
- [ unist ] : https://github.com/syntax-tree/unist
210
+ [ health ] : https://github.com/syntax-tree/.github
148
211
149
- [ node ] : https://github.com/syntax-tree/unist#node
212
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
150
213
151
- [ tree ] : https://github.com/syntax-tree/unist#tree
214
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support.md
152
215
153
- [ preorder ] : https://github.com/syntax-tree/unist#preorder
216
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
154
217
155
- [ head ] : https://github.com/syntax-tree/unist#head
218
+ [ unist ] : https://github.com/syntax-tree/unist
219
+
220
+ [ node ] : https://github.com/syntax-tree/unist#node
156
221
157
- [ type ] : https://github.com/syntax-tree/unist#type
222
+ [ preorder ] : https://github.com/syntax-tree/unist#preorder
158
223
159
- [ is ] : https://github.com/syntax-tree/unist-util-is
224
+ [ test ] : https://github.com/syntax-tree/unist-util-is#test
160
225
161
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
226
+ [ unist-util-filter ] : https://github.com/syntax-tree/unist-util-filter
162
227
163
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support.md
228
+ [ unist-util-visit ] : https://github.com/syntax-tree/unist-util-visit
164
229
165
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
230
+ [ unist-builder ] : https://github.com/syntax-tree/unist-builder
0 commit comments