8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** unist** ] [ unist ] utility to create a new [ tree] [ ] by mapping all [ node] [ ] s
12
- with the given function.
11
+ [ unist] [ ] utility to create a new tree by mapping all nodes with a given
12
+ function.
13
+
14
+ ## Contents
15
+
16
+ * [ What is this?] ( #what-is-this )
17
+ * [ When should I use this?] ( #when-should-i-use-this )
18
+ * [ Install] ( #install )
19
+ * [ Use] ( #use )
20
+ * [ API] ( #api )
21
+ * [ ` map(tree, mapFunction) ` ] ( #maptree-mapfunction )
22
+ * [ Types] ( #types )
23
+ * [ Compatibility] ( #compatibility )
24
+ * [ Related] ( #related )
25
+ * [ Contribute] ( #contribute )
26
+ * [ License] ( #license )
27
+
28
+ ## What is this?
29
+
30
+ This is a small utility that helps you make new trees.
31
+
32
+ ## When should I use this?
33
+
34
+ You can use this utility to create a new tree by mapping all nodes with a given
35
+ function.
36
+ Creating new trees like this can lead to performance problems, as it creates
37
+ new objects for every node.
38
+ When dealing with potentially large trees, and relatively few changes, use
39
+ [ ` unist-util-visit ` ] [ unist-util-visit ] (or
40
+ [ ` unist-util-visit-parents ` ] [ unist-util-visit-parents ] ) instead.
41
+
42
+ To remove certain nodes, you can also walk the tree with ` unist-util-visit ` , or
43
+ use [ ` unist-util-filter ` ] [ unist-util-filter ] (clones the tree instead of
44
+ mutating) or [ ` unist-util-remove ` ] [ unist-util-remove ] (mutates).
45
+ To create trees, use [ ` unist-builder ` ] [ unist-builder ] .
13
46
14
47
## Install
15
48
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.
18
-
19
- [ npm] [ ] :
49
+ This package is [ ESM only] [ esm ] .
50
+ In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [ npm] [ ] :
20
51
21
52
``` sh
22
53
npm install unist-util-map
23
54
```
24
55
56
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
57
+
58
+ ``` js
59
+ import {map } from " https://esm.sh/unist-util-map@3"
60
+ ```
61
+
62
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
63
+
64
+ ``` html
65
+ <script type =" module" >
66
+ import {map } from " https://esm.sh/unist-util-map@3?bundle"
67
+ </script >
68
+ ```
69
+
25
70
## Use
26
71
27
72
``` js
28
73
import {u } from ' unist-builder'
29
74
import {map } from ' unist-util-map'
30
75
31
- var tree = u (' tree' , [
76
+ const tree = u (' tree' , [
32
77
u (' leaf' , ' leaf 1' ),
33
78
u (' node' , [u (' leaf' , ' leaf 2' )]),
34
79
u (' void' ),
35
80
u (' leaf' , ' leaf 3' )
36
81
])
37
82
38
- var next = map (tree, function (node ) {
83
+ const next = map (tree, (node ) => {
39
84
return node .type === ' leaf'
40
85
? Object .assign ({}, node, {value: ' CHANGED' })
41
86
: node
@@ -58,60 +103,65 @@ Yields:
58
103
}
59
104
```
60
105
61
- …note that ` tree ` is not mutated.
106
+ > 👉 ** Note ** : ` next ` is a changed clone and ` tree ` is not mutated.
62
107
63
108
## API
64
109
65
- This package exports the following identifiers: ` map ` .
110
+ This package exports the identifier ` map ` .
66
111
There is no default export.
67
112
68
- ### ` map(tree, mapFn ) `
113
+ ### ` map(tree, mapFunction ) `
69
114
70
- Create a new [ tree] [ ] by mapping all [ node] [ ] s with the given function.
115
+ Create a new tree ([ ` Node ` ] [ node ] ) by mapping all nodes with the given function
116
+ ([ ` MapFunction ` ] [ map-function ] ).
71
117
72
- ###### Parameters
118
+ ###### Returns
73
119
74
- * ` tree ` ([ ` Node ` ] [ node ] ) — [ Tree] [ ] to map
75
- * ` callback ` ([ ` Function ` ] [ callback ] ) — Function that returns a new node
120
+ New mapped tree ([ ` Node ` ] [ node ] ).
76
121
77
- ###### Returns
122
+ #### ` function mapFunction(node, index, parent) `
78
123
79
- [ ` Node ` ] [ node ] — New mapped [ tree] [ ] .
124
+ Function called with a node ([ ` Node ` ] [ node ] ), its [ index] [ ] (` number? ` ), and its
125
+ [ parent] [ ] (` Node? ` ) to produce a new node.
80
126
81
- #### ` function mapFn(node[, index, parent]) `
127
+ ###### Returns
82
128
83
- Function called with a [ node] [ ] to produce a new node.
129
+ Node to be used in the new tree ([ ` Node ` ] [ node ] ).
130
+ The children on the returned node are not used.
131
+ if the original node has children, those are mapped instead.
84
132
85
- ###### Parameters
133
+ ## Types
86
134
87
- * ` node ` ( [ ` Node ` ] [ node ] ) — Current [ node ] [ ] being processed
88
- * ` index ` ( ` number? ` ) — [ Index ] [ ] of ` node ` , or ` null `
89
- * ` parent ` ( ` Node? ` ) — [ Parent ] [ ] of ` node ` , or ` null `
135
+ This package is fully typed with [ TypeScript ] [ ] .
136
+ It exports a type ` MapFunction<Tree extends Node = Node> ` from
137
+ ` unist-util-map/complex-types.d.ts ` to properly type ` MapFunction ` s.
90
138
91
- ###### Returns
139
+ ## Compatibility
92
140
93
- [ ` Node ` ] [ node ] — Node to be used in the new [ tree] [ ] .
94
- Its children are not used: if the original node has children, those are mapped.
141
+ Projects maintained by the unified collective are compatible with all maintained
142
+ versions of Node.js.
143
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
144
+ Our projects sometimes work with older versions, but this is not guaranteed.
95
145
96
146
## Related
97
147
98
148
* [ ` unist-util-filter ` ] ( https://github.com/syntax-tree/unist-util-filter )
99
- — Create a new tree with all nodes that pass the given function
149
+ — create a new tree with all nodes that pass the given function
100
150
* [ ` unist-util-flatmap ` ] ( https://gitlab.com/staltz/unist-util-flatmap )
101
- — Create a new tree by expanding a node into many
151
+ — create a new tree by expanding a node into many
102
152
* [ ` unist-util-remove ` ] ( https://github.com/syntax-tree/unist-util-remove )
103
- — Remove nodes from trees
153
+ — remove nodes from trees
104
154
* [ ` unist-util-select ` ] ( https://github.com/syntax-tree/unist-util-select )
105
- — Select nodes with CSS-like selectors
155
+ — select nodes with CSS-like selectors
106
156
* [ ` unist-util-visit ` ] ( https://github.com/syntax-tree/unist-util-visit )
107
- — Recursively walk over nodes
157
+ — walk trees
108
158
* [ ` unist-builder ` ] ( https://github.com/syntax-tree/unist-builder )
109
- — Creating trees
159
+ — create trees
110
160
111
161
## Contribute
112
162
113
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
114
- started.
163
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
164
+ ways to get started.
115
165
See [ ` support.md ` ] [ support ] for ways to get help.
116
166
117
167
This project has a [ code of conduct] [ coc ] .
@@ -152,24 +202,40 @@ abide by its terms.
152
202
153
203
[ npm ] : https://docs.npmjs.com/cli/install
154
204
205
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
206
+
207
+ [ esmsh ] : https://esm.sh
208
+
209
+ [ typescript ] : https://www.typescriptlang.org
210
+
155
211
[ license ] : license
156
212
157
213
[ author ] : https://efcl.info
158
214
215
+ [ health ] : https://github.com/syntax-tree/.github
216
+
217
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
218
+
219
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support.md
220
+
221
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
222
+
159
223
[ unist ] : https://github.com/syntax-tree/unist
160
224
161
225
[ node ] : https://github.com/syntax-tree/unist#node
162
226
163
- [ tree ] : https://github.com/syntax-tree/unist#tree
164
-
165
227
[ parent ] : https://github.com/syntax-tree/unist#parent-1
166
228
167
229
[ index ] : https://github.com/syntax-tree/unist#index
168
230
169
- [ callback ] : #function-mapfnnode-index-parent
231
+ [ unist-util-visit ] : https://github.com/syntax-tree/unist-util-visit
232
+
233
+ [ unist-util-visit-parents ] : https://github.com/syntax-tree/unist-util-visit-parents
234
+
235
+ [ unist-util-filter ] : https://github.com/syntax-tree/unist-util-filter
170
236
171
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
237
+ [ unist-util-remove ] : https://github.com/syntax-tree/unist-util-remove
172
238
173
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support.md
239
+ [ unist-builder ] : https://github.com/syntax-tree/unist-builder
174
240
175
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
241
+ [ map-function ] : #function-mapfunctionnode-index-parent
0 commit comments