8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- Modify direct children of a parent.
11
+ [ unist ] [ ] utility to change children of a parent.
12
12
13
- ## 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
+ * [ ` modifyChildren(modifier) ` ] ( #modifychildrenmodifier )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Related] ( #related )
24
+ * [ Contribute] ( #contribute )
25
+ * [ License] ( #license )
26
+
27
+ ## What is this?
14
28
15
- This package is [ ESM only ] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
16
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d .
29
+ This is a tiny utility that you can use to create a reusable function that
30
+ modifies children .
17
31
18
- [ npm] [ ] :
32
+ ## When should I use this?
33
+
34
+ Probably never!
35
+ Use [ ` unist-util-visit ` ] [ unist-util-visit ] .
36
+
37
+ ## Install
38
+
39
+ This package is [ ESM only] [ esm ] .
40
+ In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [ npm] [ ] :
19
41
20
42
``` sh
21
43
npm install unist-util-modify-children
22
44
```
23
45
46
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
47
+
48
+ ``` js
49
+ import {modifyChildren } from " https://esm.sh/unist-util-modify-children@3"
50
+ ```
51
+
52
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
53
+
54
+ ``` html
55
+ <script type =" module" >
56
+ import {modifyChildren } from " https://esm.sh/unist-util-modify-children@3?bundle"
57
+ </script >
58
+ ```
59
+
24
60
## Use
25
61
26
62
``` js
27
63
import u from ' unist-builder'
28
64
import {modifyChildren } from ' unist-util-modify-children'
29
65
30
- const modify = modifyChildren (modifier)
31
-
32
66
const tree = u (' root' , [
33
67
u (' leaf' , ' 1' ),
34
- u (' node ' , [u (' leaf' , ' 2' )]),
68
+ u (' parent ' , [u (' leaf' , ' 2' )]),
35
69
u (' leaf' , ' 3' )
36
70
])
71
+ const modify = modifyChildren (function (node , index , parent ) {
72
+ if (node .type === ' parent' ) {
73
+ parent .children .splice (index, 1 , {type: ' subtree' , children: parent .children })
74
+ return index + 1
75
+ }
76
+ })
37
77
38
78
modify (tree)
39
79
40
80
console .dir (tree, {depth: null })
41
-
42
- function modifier (node , index , parent ) {
43
- if (node .type === ' node' ) {
44
- parent .children .splice (index, 1 , {type: ' subtree' , children: node .children })
45
- return index + 1
46
- }
47
- }
48
81
```
49
82
50
83
Yields:
@@ -62,59 +95,74 @@ Yields:
62
95
63
96
## API
64
97
65
- This package exports the following identifiers: ` modifyChildren ` .
98
+ This package exports the identifier ` modifyChildren ` .
66
99
There is no default export.
67
100
68
- ### ` modify = modifyChildren(modifier)`
101
+ ### ` modifyChildren(modifier) `
69
102
70
- Wrap [ ` modifier ` ] [ modifier ] to be invoked for each child in the node given to
71
- [ ` modify ` ] [ modify ] .
103
+ Wrap [ ` modifier ` ] [ modifier ] to be called for each child in the nodes later given
104
+ to [ ` modify ` ] [ modify ] .
105
+
106
+ ###### Returns
107
+
108
+ [ ` Modify ` ] [ modify ] .
72
109
73
110
#### ` next? = modifier(child, index, parent) `
74
111
75
- Invoked if [ ` modify ` ] [ modify ] is called on a parent node for each ` child `
76
- in ` parent ` .
112
+ Callback called for each ` child ` in ` parent ` later given to [ ` modify ` ] [ modify ] .
77
113
78
114
###### Returns
79
115
80
- ` number ` (optional) — Next position to iterate.
116
+ Position to move to next (` number? ` ).
117
+
118
+ #### ` modify(parent) `
119
+
120
+ Call the bound [ ` modifier ` ] [ modifier ] for each child in ` parent `
121
+ ([ ` Parent ` ] [ parent ] ).
81
122
82
- #### ` function modify(parent) `
123
+ ## Types
83
124
84
- Invoke the bound [ ` modifier ` ] [ modifier ] for each child in ` parent `
85
- ([ ` Node ` ] [ node ] ).
125
+ This package is fully typed with [ TypeScript] [ ] .
126
+ It exports the additional types ` Modifier ` and ` Modify ` .
127
+
128
+ ## Compatibility
129
+
130
+ Projects maintained by the unified collective are compatible with all maintained
131
+ versions of Node.js.
132
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
133
+ Our projects sometimes work with older versions, but this is not guaranteed.
86
134
87
135
## Related
88
136
89
137
* [ ` unist-util-visit ` ] ( https://github.com/syntax-tree/unist-util-visit )
90
- — Recursively walk over nodes
138
+ — walk the tree
91
139
* [ ` unist-util-visit-parents ` ] ( https://github.com/syntax-tree/unist-util-visit-parents )
92
- — Like ` visit ` , but with a stack of parents
140
+ — walk the tree with a stack of parents
93
141
* [ ` unist-util-filter ` ] ( https://github.com/syntax-tree/unist-util-filter )
94
- — Create a new tree with all nodes that pass a test
142
+ — create a new tree with all nodes that pass a test
95
143
* [ ` unist-util-map ` ] ( https://github.com/syntax-tree/unist-util-map )
96
- — Create a new tree with all nodes mapped by a given function
144
+ — create a new tree with all nodes mapped by a given function
97
145
* [ ` unist-util-flatmap ` ] ( https://gitlab.com/staltz/unist-util-flatmap )
98
- — Create a new tree by mapping (to an array) with the given function
146
+ — create a new tree by mapping (to an array) with the given function
99
147
* [ ` unist-util-find-after ` ] ( https://github.com/syntax-tree/unist-util-find-after )
100
- — Find a node after another node
148
+ — find a node after another node
101
149
* [ ` unist-util-find-before ` ] ( https://github.com/syntax-tree/unist-util-find-before )
102
- — Find a node before another node
150
+ — find a node before another node
103
151
* [ ` unist-util-find-all-after ` ] ( https://github.com/syntax-tree/unist-util-find-all-after )
104
- — Find all nodes after another node
152
+ — find all nodes after another node
105
153
* [ ` unist-util-find-all-before ` ] ( https://github.com/syntax-tree/unist-util-find-all-before )
106
- — Find all nodes before another node
154
+ — find all nodes before another node
107
155
* [ ` unist-util-find-all-between ` ] ( https://github.com/mrzmmr/unist-util-find-all-between )
108
- — Find all nodes between two nodes
156
+ — find all nodes between two nodes
109
157
* [ ` unist-util-remove ` ] ( https://github.com/syntax-tree/unist-util-remove )
110
- — Remove nodes from a tree that pass a test
158
+ — remove nodes from a tree that pass a test
111
159
* [ ` unist-util-select ` ] ( https://github.com/syntax-tree/unist-util-select )
112
- — Select nodes with CSS-like selectors
160
+ — select nodes with CSS-like selectors
113
161
114
162
## Contribute
115
163
116
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
117
- started.
164
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
165
+ ways to get started.
118
166
See [ ` support.md ` ] [ support ] for ways to get help.
119
167
120
168
This project has a [ code of conduct] [ coc ] .
@@ -155,18 +203,30 @@ abide by its terms.
155
203
156
204
[ npm ] : https://docs.npmjs.com/cli/install
157
205
206
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
207
+
208
+ [ esmsh ] : https://esm.sh
209
+
210
+ [ typescript ] : https://www.typescriptlang.org
211
+
158
212
[ license ] : license
159
213
160
214
[ author ] : https://wooorm.com
161
215
162
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
216
+ [ health ] : https://github.com/syntax-tree/.github
163
217
164
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
218
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
165
219
166
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct .md
220
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support .md
167
221
168
- [ node ] : https://github.com/syntax-tree/unist#node
222
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
169
223
170
224
[ modifier ] : #next--modifierchild-index-parent
171
225
172
- [ modify ] : #function-modifyparent
226
+ [ modify ] : #modifyparent
227
+
228
+ [ unist ] : https://github.com/syntax-tree/unist
229
+
230
+ [ parent ] : https://github.com/syntax-tree/unist#parent
231
+
232
+ [ unist-util-visit ] : https://github.com/syntax-tree/unist-util-visit
0 commit comments