Skip to content

Commit 65725cb

Browse files
committed
Add improved docs
1 parent 43c881b commit 65725cb

File tree

1 file changed

+107
-42
lines changed

1 file changed

+107
-42
lines changed

readme.md

Lines changed: 107 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,61 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**unist**][unist] utility to modify the given tree by removing all nodes that
12-
pass the given test.
11+
[unist][] utility to remove all nodes that pass a test from the tree.
1312

14-
## 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+
* [`remove(tree[, options], test)`](#removetree-options-test)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Related](#related)
24+
* [Contribute](#contribute)
25+
* [License](#license)
26+
27+
## What is this?
28+
29+
This is a small utility that helps you clean a tree by removing some stuff.
1530

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.
31+
## When should I use this?
32+
33+
You can use this utility to remove things from a tree.
34+
This utility is very similar to [`unist-util-filter`][unist-util-filter], which
35+
creates a new tree.
36+
Modifying a tree like this utility `unist-util-remove` does is much faster on
37+
larger documents though.
38+
39+
You can also walk the tree with [`unist-util-visit`][unist-util-visit] to remove
40+
nodes.
41+
To create trees, use [`unist-builder`][unist-builder].
42+
43+
## Install
1844

19-
[npm][]:
45+
This package is [ESM only][esm].
46+
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
2047

2148
```sh
2249
npm install unist-util-remove
2350
```
2451

52+
In Deno with [`esm.sh`][esmsh]:
53+
54+
```js
55+
import {remove} from "https://esm.sh/unist-util-remove@3"
56+
```
57+
58+
In browsers with [`esm.sh`][esmsh]:
59+
60+
```html
61+
<script type="module">
62+
import {remove} from "https://esm.sh/unist-util-remove@3?bundle"
63+
</script>
64+
```
65+
2566
## Use
2667

2768
```js
@@ -30,10 +71,10 @@ import {remove} from 'unist-util-remove'
3071

3172
const tree = u('root', [
3273
u('leaf', '1'),
33-
u('node', [
74+
u('parent', [
3475
u('leaf', '2'),
35-
u('node', [u('leaf', '3'), u('other', '4')]),
36-
u('node', [u('leaf', '5')])
76+
u('parent', [u('leaf', '3'), u('other', '4')]),
77+
u('parent', [u('leaf', '5')])
3778
]),
3879
u('leaf', '6')
3980
])
@@ -44,64 +85,80 @@ remove(tree, 'leaf')
4485
console.dir(tree, {depth: null})
4586
```
4687

47-
Yields: (note the parent of `5` is also removed, due to `options.cascade`)
88+
Yields:
4889

4990
```js
5091
{
5192
type: 'root',
5293
children: [
5394
{
54-
type: 'node',
95+
type: 'parent',
5596
children: [{type: 'node', children: [{type: 'other', value: '4'}]}]
5697
}
5798
]
5899
}
59100
```
60101

102+
> 👉 **Note**: the parent of leaf `5` is also removed, `options.cascade` can
103+
> change that.
104+
61105
## API
62106

63-
This package exports the following identifiers: `remove`.
107+
This package exports the identifier `remove`.
64108
There is no default export.
65109

66110
### `remove(tree[, options], test)`
67111

68-
Mutate the given [tree][] by removing all nodes that pass `test`.
112+
Mutate the given `tree` ([`Node`][node]) by removing all nodes that pass `test`
113+
(`Test` from [`unist-util-is`][test]).
69114
The tree is walked in [preorder][] (NLR), visiting the node itself, then its
70-
[head][], etc.
115+
head, etc.
71116

72-
###### Parameters
117+
##### `options`
73118

74-
* `tree` ([`Node?`][node])
75-
[Tree][] to filter
76-
* `options.cascade` (`boolean`, default: `true`)
77-
— Whether to drop parent nodes if they had children, but all their children
78-
were filtered out
79-
* `test` ([`Test`][is]) — [`is`][is]-compatible test (such as a [type][])
119+
Configuration (optional).
80120

81-
###### Returns
121+
###### `options.cascade`
82122

83-
[`Node?`][node] — The given `tree` with nodes for which `test` passed removed.
84-
`null` is returned if `tree` itself didn’t pass the test, or is cascaded away.
123+
Whether to drop parent nodes if they had children, but all their children were
124+
filtered out (`boolean`, default: `true`).
125+
126+
##### Returns
127+
128+
The given `tree` without nodes that pass `test` ([`Node?`][node]).
129+
`null` is returned if `tree` itself didn’t pass the test or is cascaded away.
130+
131+
## Types
132+
133+
This package is fully typed with [TypeScript][].
134+
It exports no additional types (types for the test are in `unist-util-is`).
135+
136+
## Compatibility
137+
138+
Projects maintained by the unified collective are compatible with all maintained
139+
versions of Node.js.
140+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
141+
Our projects sometimes work with older versions, but this is not guaranteed.
85142

86143
## Related
87144

88145
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)
89-
Create a new tree with all nodes that pass the given function
146+
create a new tree with all nodes that pass the given function
90147
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
91-
Create a new tree by expanding a node into many
148+
create a new tree by expanding a node into many
92149
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
93-
Create a new tree by mapping nodes
150+
create a new tree by mapping nodes
94151
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
95-
Select nodes with CSS-like selectors
152+
select nodes with CSS-like selectors
96153
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
97-
Recursively walk over nodes
154+
— walk the tree
98155
* [`unist-builder`](https://github.com/syntax-tree/unist-builder)
99-
Creating trees
156+
create trees
100157

101158
## Contribute
102159

103-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
104-
started.
160+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
161+
ways to get started.
105162
See [`support.md`][support] for ways to get help.
106163

107164
This project has a [Code of Conduct][coc].
@@ -142,24 +199,32 @@ abide by its terms.
142199

143200
[npm]: https://docs.npmjs.com/cli/install
144201

202+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
203+
204+
[esmsh]: https://esm.sh
205+
206+
[typescript]: https://www.typescriptlang.org
207+
145208
[license]: license
146209

147-
[unist]: https://github.com/syntax-tree/unist
210+
[health]: https://github.com/syntax-tree/.github
148211

149-
[node]: https://github.com/syntax-tree/unist#node
212+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
150213

151-
[tree]: https://github.com/syntax-tree/unist#tree
214+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
152215

153-
[preorder]: https://github.com/syntax-tree/unist#preorder
216+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
154217

155-
[head]: https://github.com/syntax-tree/unist#head
218+
[unist]: https://github.com/syntax-tree/unist
219+
220+
[node]: https://github.com/syntax-tree/unist#node
156221

157-
[type]: https://github.com/syntax-tree/unist#type
222+
[preorder]: https://github.com/syntax-tree/unist#preorder
158223

159-
[is]: https://github.com/syntax-tree/unist-util-is
224+
[test]: https://github.com/syntax-tree/unist-util-is#test
160225

161-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
226+
[unist-util-filter]: https://github.com/syntax-tree/unist-util-filter
162227

163-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
228+
[unist-util-visit]: https://github.com/syntax-tree/unist-util-visit
164229

165-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
230+
[unist-builder]: https://github.com/syntax-tree/unist-builder

0 commit comments

Comments
 (0)