1
- # unist-util-remove [ ![ Build Status ] [ build-badge ]] [ build-page ] [ ![ Coverage Status ] [ coverage-badge ]] [ coverage-page ]
1
+ # unist-util-remove
2
2
3
- Modify the given [ unist] [ ] tree to remove 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 modify the given tree by removing all nodes that
9
+ pass the given test.
4
10
5
11
## Install
6
12
13
+ [ npm] [ ] :
14
+
7
15
``` sh
8
16
npm install unist-util-remove
9
17
```
@@ -27,80 +35,115 @@ var tree = u('root', [
27
35
// Remove all nodes of type `leaf`.
28
36
remove (tree, ' leaf' )
29
37
30
- console .log (tree)
38
+ console .dir (tree, {depth : null } )
31
39
```
32
40
33
41
Yields: (note the parent of ` 5 ` is also removed, due to ` opts.cascade ` )
34
42
35
43
``` js
36
44
{
37
45
type: ' root' ,
38
- children: [{
39
- type: ' node' ,
40
- children: [{
46
+ children: [
47
+ {
41
48
type: ' node' ,
42
- children: [{
43
- type: ' other' ,
44
- value: ' 4'
45
- }]
46
- }]
47
- }]
49
+ children: [
50
+ { type: ' node' , children: [ { type: ' other' , value: ' 4' } ] }
51
+ ]
52
+ }
53
+ ]
48
54
}
49
55
```
50
56
51
57
## API
52
58
53
- ### ` remove(tree, [opts] , test) `
59
+ ### ` remove(tree[, options][ , test] ) `
54
60
55
- Mutate ` tree ` by removing all nodes that pass ` test ` .
56
- The tree is filtered in [ preorder] [ ] .
61
+ Mutate the given [ tree] [ ] by removing all nodes that pass ` test ` .
62
+ The tree is walked in [ preorder] [ ] (NLR), visiting the node itself, then its
63
+ [ head] [ ] , etc.
57
64
58
65
###### Parameters
59
66
60
67
* ` tree ` ([ ` Node? ` ] [ node ] )
61
- — Tree to filter
62
- * ` opts .cascade` (` boolean ` , default: ` true ` )
63
- — Whether to drop parent nodes if they had children, but all their
64
- children were filtered out
65
- * ` test `
66
- — See [ ` unist-util-is ` ] [ is ] for details
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 ] [ ] )
67
74
68
75
###### Returns
69
76
70
- The given ` tree ` ([ ` Node? ` ] [ node ] ) with nodes for which ` test ` returned ` true `
71
- removed. ` null ` is returned if ` tree ` itself didn’t pass the test, or is
72
- cascaded away.
77
+ [ ` Node? ` ] [ node ] — The given ` tree ` with nodes for which ` test ` passed removed.
78
+ ` null ` is returned if ` tree ` itself didn’t pass the test, or is cascaded away.
79
+
80
+ ## Related
81
+
82
+ * [ ` unist-util-filter ` ] ( https://github.com/syntax-tree/unist-util-filter )
83
+ — Create a new tree with all nodes that pass the given function
84
+ * [ ` unist-util-flatmap ` ] ( https://gitlab.com/staltz/unist-util-flatmap )
85
+ — Create a new tree by expanding a node into many
86
+ * [ ` unist-util-map ` ] ( https://github.com/syntax-tree/unist-util-map )
87
+ — Create a new tree by mapping nodes
88
+ * [ ` unist-util-select ` ] ( https://github.com/syntax-tree/unist-util-select )
89
+ — Select nodes with CSS-like selectors
90
+ * [ ` unist-util-visit ` ] ( https://github.com/syntax-tree/unist-util-visit )
91
+ — Recursively walk over nodes
92
+ * [ ` unist-builder ` ] ( https://github.com/syntax-tree/unist-builder )
93
+ — Creating trees
73
94
74
95
## Contribute
75
96
76
- See [ ` contributing.md ` in ` syntax-tree/unist ` ] [ contributing ] for ways to get
97
+ See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
77
98
started.
99
+ See [ ` support.md ` ] [ support ] for ways to get help.
78
100
79
- This organisation has a [ Code of Conduct] [ coc ] . By interacting with this
80
- repository, organisation, or community you agree to abide by its terms.
101
+ This project has a [ Code of Conduct] [ coc ] .
102
+ By interacting with this repository, organisation, or community you agree to
103
+ abide by its terms.
81
104
82
105
## License
83
106
84
- [ MIT] [ ] © Eugene Sharygin
107
+ [ MIT] [ license ] © Eugene Sharygin
108
+
109
+ <!-- Definitions -->
110
+
111
+ [ build-badge ] : https://img.shields.io/travis/syntax-tree/unist-util-filter.svg
112
+
113
+ [ build ] : https://travis-ci.org/syntax-tree/unist-util-filter
114
+
115
+ [ coverage-badge ] : https://img.shields.io/codecov/c/github/syntax-tree/unist-util-filter.svg
85
116
86
- [ mit ] : license
117
+ [ coverage ] : https://codecov.io/github/syntax-tree/unist-util-filter
118
+
119
+ [ downloads-badge ] : https://img.shields.io/npm/dm/unist-util-filter.svg
120
+
121
+ [ downloads ] : https://www.npmjs.com/package/unist-util-filter
122
+
123
+ [ size-badge ] : https://img.shields.io/bundlephobia/minzip/unist-util-filter.svg
124
+
125
+ [ size ] : https://bundlephobia.com/result?p=unist-util-filter
126
+
127
+ [ npm ] : https://docs.npmjs.com/cli/install
128
+
129
+ [ license ] : license
87
130
88
131
[ unist ] : https://github.com/syntax-tree/unist
89
132
90
133
[ node ] : https://github.com/syntax-tree/unist#node
91
134
92
- [ is ] : https://github.com/syntax-tree/unist-util-is
135
+ [ tree ] : https://github.com/syntax-tree/unist#tree
93
136
94
- [ preorder ] : https://en.wikipedia .org/wiki/Tree_traversal
137
+ [ preorder ] : https://www.geeksforgeeks .org/tree-traversals-inorder-preorder-and-postorder/
95
138
96
- [ build-page ] : https://travis-ci.org /syntax-tree/unist-util-remove
139
+ [ head ] : https://github.com /syntax-tree/unist#head
97
140
98
- [ build-badge ] : https://travis-ci.org /syntax-tree/unist-util-remove.svg?branch=master
141
+ [ type ] : https://github.com /syntax-tree/unist#type
99
142
100
- [ coverage-page ] : https://codecov.io/github/ syntax-tree/unist-util-remove?branch=master
143
+ [ is ] : https://github.com/ syntax-tree/unist-util-is
101
144
102
- [ coverage-badge ] : https://img.shields.io/codecov/c/github/ syntax-tree/unist-util-remove.svg?branch= master
145
+ [ contributing ] : https://github.com/ syntax-tree/.github/blob/ master/contributing.md
103
146
104
- [ contributing ] : https://github.com/syntax-tree/unist /blob/master/contributing .md
147
+ [ support ] : https://github.com/syntax-tree/.github /blob/master/support .md
105
148
106
- [ coc ] : https://github.com/syntax-tree/unist /blob/master/code-of-conduct.md
149
+ [ coc ] : https://github.com/syntax-tree/.github /blob/master/code-of-conduct.md
0 commit comments