Skip to content

Commit 2f26901

Browse files
committed
Add improved docs
1 parent feb9117 commit 2f26901

File tree

1 file changed

+91
-43
lines changed

1 file changed

+91
-43
lines changed

readme.md

Lines changed: 91 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,65 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

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.
1212

13-
Instead of modifying the original syntax tree, this module returns a wrapper
14-
that makes it easier to traverse that tree.
13+
## Contents
1514

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)
1926

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.
2131

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?
2433

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][]:
2644

2745
```sh
2846
npm install unist-util-parents
2947
```
3048

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+
3163
## Use
3264

3365
```js
3466
import {u} from 'unist-builder'
3567
import {parents} from 'unist-util-parents'
3668

37-
var tree = u('root', [
69+
const tree = u('root', [
3870
u('leaf', 'leaf 1'),
3971
u('node', [
4072
u('leaf', 'leaf 2'),
@@ -48,18 +80,18 @@ var tree = u('root', [
4880
])
4981
])
5082

51-
var wrapped = parents(tree)
83+
const wrapped = parents(tree)
5284

5385
// 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]
5587

56-
var chain = []
88+
const chain = []
5789
while (node) {
58-
chain.unshift(node.type)
90+
chain.push(node.type)
5991
node = node.parent
6092
}
6193

62-
console.log(chain)
94+
console.log(chain.reverse())
6395
```
6496

6597
Yields:
@@ -70,47 +102,57 @@ Yields:
70102

71103
## API
72104

73-
This package exports the following identifiers: `parents`.
105+
This package exports the identifier `parents`.
74106
There is no default export.
75107

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.
77112

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:
80114

81-
* `parent` — parent link (or `null` for the root node)
115+
* `parent` — parent link (or `null` for the root)
82116
* `node` — link to the original node
83117

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.
86120

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.
89123

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.
91125

92-
###### Parameters
126+
###### Returns
93127

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.
95131

96-
###### Returns
132+
## Types
133+
134+
This package is fully typed with [TypeScript][].
135+
It exports the additional type `Proxy`.
136+
137+
## Compatibility
97138

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.
101143

102144
## Related
103145

104146
* [`unist-util-visit-parents`][unist-util-visit-parents]
105-
Recursively walk over unist nodes, with ancestral information
147+
— walk the tree with ancestral information
106148

107149
## Contribute
108150

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.
111153
See [`support.md`][support] for ways to get help.
112154

113-
This project has a [Code of Conduct][coc].
155+
This project has a [code of conduct][coc].
114156
By interacting with this repository, organisation, or community you agree to
115157
abide by its terms.
116158

@@ -148,18 +190,24 @@ abide by its terms.
148190

149191
[npm]: https://docs.npmjs.com/cli/install
150192

193+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
194+
195+
[esmsh]: https://esm.sh
196+
197+
[typescript]: https://www.typescriptlang.org
198+
151199
[license]: license
152200

153-
[unist]: https://github.com/syntax-tree/unist
201+
[health]: https://github.com/syntax-tree/.github
154202

155-
[node]: https://github.com/syntax-tree/unist#node
203+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
156204

157-
[tree]: https://github.com/syntax-tree/unist#tree
205+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
158206

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
160208

161-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
209+
[unist]: https://github.com/syntax-tree/unist
162210

163-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
211+
[node]: https://github.com/syntax-tree/unist#node
164212

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

Comments
 (0)