8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** unist** ] [ unist ] utility to add references to parents on nodes in a tree.
11
+ [ unist] [ ] utility to add references to parents on nodes in a tree.
12
12
13
- Instead of modifying the original syntax tree, this module returns a wrapper
14
- that makes it easier to traverse that tree.
13
+ ## Contents
15
14
16
- Algorithms that work on regular unist trees are (mostly) guaranteed to work on
17
- wrapped trees, and each wrapped node maintains a reference to the node from
18
- which it originated.
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
+ * [ ` parents(node) ` ] ( #parentsnode )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Related] ( #related )
24
+ * [ Contribute] ( #contribute )
25
+ * [ License] ( #license )
19
26
20
- ## Install
27
+ ## What is this?
28
+
29
+ This utility creates a proxy of the tree that acts like the original tree upon
30
+ reading, but each proxied node has a reference to its parent node.
21
31
22
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
23
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
32
+ ## When should I use this?
24
33
25
- [ npm] [ ] :
34
+ This package can be very useful for problems where it is needed to figure out
35
+ what a nodes ancestors are, because unist itself is a non-cyclical data
36
+ structure, and thus does not provide that information.
37
+ On the other hand, this info on ancestors can also be gathered when walking the
38
+ tree with [ ` unist-util-visit-parents ` ] [ unist-util-visit-parents ] .
39
+
40
+ ## Install
41
+
42
+ This package is [ ESM only] [ esm ] .
43
+ In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [ npm] [ ] :
26
44
27
45
``` sh
28
46
npm install unist-util-parents
29
47
```
30
48
49
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
50
+
51
+ ``` js
52
+ import {parent } from " https://esm.sh/unist-util-parents@2"
53
+ ```
54
+
55
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
56
+
57
+ ``` html
58
+ <script type =" module" >
59
+ import {parent } from " https://esm.sh/unist-util-parents@2?bundle"
60
+ </script >
61
+ ```
62
+
31
63
## Use
32
64
33
65
``` js
34
66
import {u } from ' unist-builder'
35
67
import {parents } from ' unist-util-parents'
36
68
37
- var tree = u (' root' , [
69
+ const tree = u (' root' , [
38
70
u (' leaf' , ' leaf 1' ),
39
71
u (' node' , [
40
72
u (' leaf' , ' leaf 2' ),
@@ -48,18 +80,18 @@ var tree = u('root', [
48
80
])
49
81
])
50
82
51
- var wrapped = parents (tree)
83
+ const wrapped = parents (tree)
52
84
53
85
// Leaf 4
54
- var node = wrapped .children [1 ].children [2 ].children [1 ].children [0 ]
86
+ const node = wrapped .children [1 ].children [2 ].children [1 ].children [0 ]
55
87
56
- var chain = []
88
+ const chain = []
57
89
while (node) {
58
- chain .unshift (node .type )
90
+ chain .push (node .type )
59
91
node = node .parent
60
92
}
61
93
62
- console .log (chain)
94
+ console .log (chain . reverse () )
63
95
```
64
96
65
97
Yields:
@@ -70,47 +102,57 @@ Yields:
70
102
71
103
## API
72
104
73
- This package exports the following identifiers: ` parents ` .
105
+ This package exports the identifier ` parents ` .
74
106
There is no default export.
75
107
76
- ### ` parents(tree) `
108
+ ### ` parents(node) `
109
+
110
+ Create a proxy of ` node ` ([ ` Node ` ] [ node ] ) that acts like the original tree upon
111
+ reading, but each proxied node has a reference to its parent node.
77
112
78
- Returns a wrapped ` tree ` with a proxy that imposes two additional properties on
79
- all of its nodes:
113
+ The returned proxy imposes two additional fields on all of its nodes:
80
114
81
- * ` parent ` — parent link (or ` null ` for the root node )
115
+ * ` parent ` — parent link (or ` null ` for the root)
82
116
* ` node ` — link to the original node
83
117
84
- None of these properties are enumerable, and the original tree is * not changed* .
85
- This means you can ` JSON.stringify ` the wrapped tree and it’s the same.
118
+ These new fields are not enumerable and the original tree is * not changed* .
119
+ This means you can use ` JSON.stringify ` on the wrapped tree and it’s the same.
86
120
87
- ` wrapped.children ` returns array of wrapped child nodes, so that any
88
- recursive algorithm will work on a wrapped tree just as well.
121
+ ` wrapped.children ` returns array of wrapped child nodes, so that any recursive
122
+ algorithm will work on a wrapped tree just as well.
89
123
90
- Remember to access ` .node ` before you commit any changes to a node .
124
+ To write changes to the tree, use ` .node ` to access the original tree .
91
125
92
- ###### Parameters
126
+ ###### Returns
93
127
94
- * ` tree ` ([ ` Node ` ] [ node ] ) — [ Tree] [ ] to wrap
128
+ A wrapped node ([ ` Node ` ] [ node ] ), which is a shallow copy of the given node that
129
+ also includes non-enumerable references to ` node ` and ` parent ` , and if ` tree `
130
+ had children, they are wrapped as well.
95
131
96
- ###### Returns
132
+ ## Types
133
+
134
+ This package is fully typed with [ TypeScript] [ ] .
135
+ It exports the additional type ` Proxy ` .
136
+
137
+ ## Compatibility
97
138
98
- [ ` Node ` ] [ node ] — A wrapped node: shallow copy of the given node with
99
- non-enumerable references to ` node ` and ` parent ` , and if ` tree ` had children,
100
- they are wrapped as well.
139
+ Projects maintained by the unified collective are compatible with all maintained
140
+ versions of Node.js.
141
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
142
+ Our projects sometimes work with older versions, but this is not guaranteed.
101
143
102
144
## Related
103
145
104
146
* [ ` unist-util-visit-parents ` ] [ unist-util-visit-parents ]
105
- — Recursively walk over unist nodes, with ancestral information
147
+ — walk the tree with ancestral information
106
148
107
149
## Contribute
108
150
109
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
110
- started.
151
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
152
+ ways to get started.
111
153
See [ ` support.md ` ] [ support ] for ways to get help.
112
154
113
- This project has a [ Code of Conduct ] [ coc ] .
155
+ This project has a [ code of conduct ] [ coc ] .
114
156
By interacting with this repository, organisation, or community you agree to
115
157
abide by its terms.
116
158
@@ -148,18 +190,24 @@ abide by its terms.
148
190
149
191
[ npm ] : https://docs.npmjs.com/cli/install
150
192
193
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
194
+
195
+ [ esmsh ] : https://esm.sh
196
+
197
+ [ typescript ] : https://www.typescriptlang.org
198
+
151
199
[ license ] : license
152
200
153
- [ unist ] : https://github.com/syntax-tree/unist
201
+ [ health ] : https://github.com/syntax-tree/.github
154
202
155
- [ node ] : https://github.com/syntax-tree/unist#node
203
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
156
204
157
- [ tree ] : https://github.com/syntax-tree/unist#tree
205
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support.md
158
206
159
- [ unist-util-visit-parents ] : https://github.com/syntax-tree/unist-util-visit-parents
207
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
160
208
161
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
209
+ [ unist ] : https://github.com/syntax-tree/unist
162
210
163
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support.md
211
+ [ node ] : https://github.com/syntax-tree/unist#node
164
212
165
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
213
+ [ unist-util-visit-parents ] : https://github.com/syntax-tree/unist-util-visit-parents
0 commit comments