Skip to content

Commit 8bc34ee

Browse files
committed
Add improved docs
1 parent 58daa78 commit 8bc34ee

File tree

3 files changed

+124
-57
lines changed

3 files changed

+124
-57
lines changed

index.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,40 @@
33
* @typedef {import('unist').Node} Node
44
*
55
* @callback Modifier
6+
* Callback called for each `child` in `parent` later given to `modify`.
67
* @param {Node} node
8+
* Child of `parent`.
79
* @param {number} index
10+
* Position of `child` in `parent`.
811
* @param {Parent} parent
12+
* Parent node.
913
* @returns {number|void}
14+
* Position to move to next.
1015
*
1116
* @callback Modify
17+
* Modify children of `parent`.
1218
* @param {Parent} node
19+
* Parent node.
1320
* @returns {void}
21+
* Nothing.
1422
*/
1523

1624
import {arrayIterate} from 'array-iterate'
1725

1826
/**
19-
* Turn `callback` into a child-modifier accepting a parent.
20-
* See `array-iterate` for more info.
27+
* Wrap `modifier` to be called for each child in the nodes later given to
28+
* `modify`.
2129
*
22-
* @param {Modifier} callback
30+
* @param {Modifier} modifier
31+
* Callback called for each `child` in `parent` later given to `modify`.
2332
* @returns {Modify}
33+
* Modify children of `parent`.
2434
*/
25-
export function modifyChildren(callback) {
26-
return iterator
35+
export function modifyChildren(modifier) {
36+
return modify
2737

28-
/**
29-
* @param {Parent} parent
30-
* @returns {void}
31-
*/
32-
function iterator(parent) {
38+
/** @type {Modify} */
39+
function modify(parent) {
3340
if (!parent || !parent.children) {
3441
throw new Error('Missing children in `parent` for `modifier`')
3542
}
@@ -38,13 +45,13 @@ export function modifyChildren(callback) {
3845
}
3946

4047
/**
41-
* Pass the context as the third argument to `callback`.
48+
* Pass the context as the third argument to `modifier`.
4249
*
4350
* @this {Parent}
4451
* @param {Node} node
4552
* @param {number} index
4653
*/
4754
function iteratee(node, index) {
48-
return callback(node, index, this)
55+
return modifier(node, index, this)
4956
}
5057
}

readme.md

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

11-
Modify direct children of a parent.
11+
[unist][] utility to change children of a parent.
1212

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

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

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

2042
```sh
2143
npm install unist-util-modify-children
2244
```
2345

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+
2460
## Use
2561

2662
```js
2763
import u from 'unist-builder'
2864
import {modifyChildren} from 'unist-util-modify-children'
2965

30-
const modify = modifyChildren(modifier)
31-
3266
const tree = u('root', [
3367
u('leaf', '1'),
34-
u('node', [u('leaf', '2')]),
68+
u('parent', [u('leaf', '2')]),
3569
u('leaf', '3')
3670
])
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+
})
3777

3878
modify(tree)
3979

4080
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-
}
4881
```
4982

5083
Yields:
@@ -62,59 +95,74 @@ Yields:
6295

6396
## API
6497

65-
This package exports the following identifiers: `modifyChildren`.
98+
This package exports the identifier `modifyChildren`.
6699
There is no default export.
67100

68-
### `modify = modifyChildren(modifier)`
101+
### `modifyChildren(modifier)`
69102

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

73110
#### `next? = modifier(child, index, parent)`
74111

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

78114
###### Returns
79115

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

82-
#### `function modify(parent)`
123+
## Types
83124

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

87135
## Related
88136

89137
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
90-
Recursively walk over nodes
138+
— walk the tree
91139
* [`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
93141
* [`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
95143
* [`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
97145
* [`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
99147
* [`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
101149
* [`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
103151
* [`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
105153
* [`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
107155
* [`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
109157
* [`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
111159
* [`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
113161

114162
## Contribute
115163

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

120168
This project has a [code of conduct][coc].
@@ -155,18 +203,30 @@ abide by its terms.
155203

156204
[npm]: https://docs.npmjs.com/cli/install
157205

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

160214
[author]: https://wooorm.com
161215

162-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
216+
[health]: https://github.com/syntax-tree/.github
163217

164-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
218+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
165219

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
167221

168-
[node]: https://github.com/syntax-tree/unist#node
222+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
169223

170224
[modifier]: #next--modifierchild-index-parent
171225

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

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test('modifyChildren()', (t) => {
2727
'should throw without parent'
2828
)
2929

30-
t.test('should invoke `fn` for each child in `parent`', (st) => {
30+
t.test('should call `fn` for each child in `parent`', (st) => {
3131
const children = [
3232
{type: 'x', value: 0},
3333
{type: 'x', value: 1},

0 commit comments

Comments
 (0)