Skip to content

Commit 90e8d93

Browse files
committed
Add improved docs
1 parent 9e83fbc commit 90e8d93

File tree

2 files changed

+117
-46
lines changed

2 files changed

+117
-46
lines changed

index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
*/
44

55
/**
6-
* Unist utility to create a new tree by mapping all nodes with the given function.
6+
* Create a new tree by mapping all nodes with the given function.
77
*
88
* @template {Node} Tree
9-
* @param {Tree} tree Tree to map
10-
* @param {import('./complex-types').MapFunction<Tree>} iteratee Function that returns a new node
11-
* @returns {Tree} New mapped tree.
9+
* Type of input tree.
10+
* @param {Tree} tree
11+
* Tree to map.
12+
* @param {import('./complex-types').MapFunction<Tree>} mapFunction
13+
* Function called with a node, its index, and its parent to produce a new
14+
* node.
15+
* @returns {Tree}
16+
* New mapped tree.
1217
*/
13-
export function map(tree, iteratee) {
18+
export function map(tree, mapFunction) {
1419
// @ts-expect-error Looks like a children.
1520
return preorder(tree, null, null)
1621

1722
/** @type {import('./complex-types').MapFunction<Tree>} */
1823
function preorder(node, index, parent) {
19-
var newNode = Object.assign({}, iteratee(node, index, parent))
24+
var newNode = Object.assign({}, mapFunction(node, index, parent))
2025

2126
if ('children' in node) {
2227
// @ts-expect-error Looks like a parent.

readme.md

Lines changed: 106 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,79 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

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].
1346

1447
## Install
1548

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

2152
```sh
2253
npm install unist-util-map
2354
```
2455

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+
2570
## Use
2671

2772
```js
2873
import {u} from 'unist-builder'
2974
import {map} from 'unist-util-map'
3075

31-
var tree = u('tree', [
76+
const tree = u('tree', [
3277
u('leaf', 'leaf 1'),
3378
u('node', [u('leaf', 'leaf 2')]),
3479
u('void'),
3580
u('leaf', 'leaf 3')
3681
])
3782

38-
var next = map(tree, function(node) {
83+
const next = map(tree, (node) => {
3984
return node.type === 'leaf'
4085
? Object.assign({}, node, {value: 'CHANGED'})
4186
: node
@@ -58,60 +103,65 @@ Yields:
58103
}
59104
```
60105

61-
…note that `tree` is not mutated.
106+
> 👉 **Note**: `next` is a changed clone and `tree` is not mutated.
62107
63108
## API
64109

65-
This package exports the following identifiers: `map`.
110+
This package exports the identifier `map`.
66111
There is no default export.
67112

68-
### `map(tree, mapFn)`
113+
### `map(tree, mapFunction)`
69114

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]).
71117

72-
###### Parameters
118+
###### Returns
73119

74-
* `tree` ([`Node`][node]) — [Tree][] to map
75-
* `callback` ([`Function`][callback]) — Function that returns a new node
120+
New mapped tree ([`Node`][node]).
76121

77-
###### Returns
122+
#### `function mapFunction(node, index, parent)`
78123

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

81-
#### `function mapFn(node[, index, parent])`
127+
###### Returns
82128

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

85-
###### Parameters
133+
## Types
86134

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

91-
###### Returns
139+
## Compatibility
92140

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

96146
## Related
97147

98148
* [`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
100150
* [`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
102152
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)
103-
Remove nodes from trees
153+
remove nodes from trees
104154
* [`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
106156
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
107-
Recursively walk over nodes
157+
— walk trees
108158
* [`unist-builder`](https://github.com/syntax-tree/unist-builder)
109-
Creating trees
159+
create trees
110160

111161
## Contribute
112162

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

117167
This project has a [code of conduct][coc].
@@ -152,24 +202,40 @@ abide by its terms.
152202

153203
[npm]: https://docs.npmjs.com/cli/install
154204

205+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
206+
207+
[esmsh]: https://esm.sh
208+
209+
[typescript]: https://www.typescriptlang.org
210+
155211
[license]: license
156212

157213
[author]: https://efcl.info
158214

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+
159223
[unist]: https://github.com/syntax-tree/unist
160224

161225
[node]: https://github.com/syntax-tree/unist#node
162226

163-
[tree]: https://github.com/syntax-tree/unist#tree
164-
165227
[parent]: https://github.com/syntax-tree/unist#parent-1
166228

167229
[index]: https://github.com/syntax-tree/unist#index
168230

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
170236

171-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
237+
[unist-util-remove]: https://github.com/syntax-tree/unist-util-remove
172238

173-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
239+
[unist-builder]: https://github.com/syntax-tree/unist-builder
174240

175-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
241+
[map-function]: #function-mapfunctionnode-index-parent

0 commit comments

Comments
 (0)