Skip to content

Commit 54f0a5b

Browse files
committed
Add improved docs
1 parent 23a1238 commit 54f0a5b

File tree

3 files changed

+124
-55
lines changed

3 files changed

+124
-55
lines changed

index.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,49 @@
22
* @typedef {import('unist').Node} Node
33
* @typedef {import('unist').Parent} Parent
44
* @typedef {import('unist-util-is').Test} Test
5-
*/
6-
7-
/**
8-
* Options for unist util filter
95
*
10-
* @typedef {Object} FilterOptions
11-
* @property {boolean} [cascade=true] Whether to drop parent nodes if they had children, but all their children were filtered out.
6+
* @typedef Options
7+
* Configuration (optional).
8+
* @property {boolean} [cascade=true]
9+
* Whether to drop parent nodes if they had children, but all their children
10+
* were filtered out.
11+
*
12+
* @typedef {Options} FilterOptions
13+
* Deprecated, use `Options`.
1214
*/
1315

1416
import {convert} from 'unist-util-is'
1517

1618
const own = {}.hasOwnProperty
1719

1820
/**
19-
* Create a new tree consisting of copies of all nodes that pass test.
20-
* The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.
21+
* Create a new `tree` of copies of all nodes that pass `test`.
22+
* The tree is walked in preorder (NLR), visiting the node itself, then its
23+
* head, etc.
2124
*
22-
* @param tree Tree to filter.
23-
* @param options Configuration (optional).
24-
* @param test is-compatible test (such as a type).
25-
* @returns Given `tree` or `null` if it didn’t pass `test`.
25+
* @param tree
26+
* Tree to filter.
27+
* @param options
28+
* Configuration (optional).
29+
* @param test
30+
* `unist-util-is`-compatible test (such as a type).
31+
* @returns
32+
* New filtered tree.
33+
* `null` is returned if `tree` itself didn’t pass the test, or is cascaded
34+
* away.
2635
*/
2736
export const filter =
2837
/**
2938
* @type {(
30-
* (<Tree extends Node, Check extends Test>(node: Tree, options: FilterOptions, test: Check) => import('./complex-types').Matches<Tree, Check>) &
39+
* (<Tree extends Node, Check extends Test>(node: Tree, options: Options, test: Check) => import('./complex-types').Matches<Tree, Check>) &
3140
* (<Tree extends Node, Check extends Test>(node: Tree, test: Check) => import('./complex-types').Matches<Tree, Check>) &
32-
* (<Tree extends Node>(node: Tree, options?: FilterOptions) => Tree)
41+
* (<Tree extends Node>(node: Tree, options?: Options) => Tree)
3342
* )}
3443
*/
3544
(
3645
/**
3746
* @param {Node} tree
38-
* @param {FilterOptions} options
47+
* @param {Options} options
3948
* @param {Test} test
4049
* @returns {Node|null}
4150
*/
@@ -55,7 +64,7 @@ export const filter =
5564
* @returns {Node|null}
5665
*/
5766
function preorder(node, index, parent) {
58-
/** @type {Array.<Node>} */
67+
/** @type {Array<Node>} */
5968
const children = []
6069
/** @type {number} */
6170
let childIndex

readme.md

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

11-
[**unist**][unist] utility to create a new tree with all nodes that pass the
12-
given test.
11+
[unist][] utility to create a new tree with only nodes that pass a test.
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+
* [`filter(tree[, options][, test])`](#filtertree-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.
30+
31+
## When should I use this?
1532

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.
33+
You can use this utility to remove things from a tree.
34+
This utility is very similar to [`unist-util-remove`][unist-util-remove], which
35+
changes the given tree.
36+
Modifying a tree like that utility does is much faster on larger documents.
1837

19-
[npm][]:
38+
You can also walk the tree with [`unist-util-visit`][unist-util-visit] to remove
39+
nodes.
40+
To create trees, use [`unist-builder`][unist-builder].
41+
42+
## Install
43+
44+
This package is [ESM only][esm].
45+
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
2046

2147
```sh
2248
npm install unist-util-filter
2349
```
2450

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

2767
```js
@@ -30,7 +70,7 @@ import {filter} from 'unist-util-filter'
3070

3171
const tree = u('root', [
3272
u('leaf', '1'),
33-
u('node', [u('leaf', '2'), u('node', [u('leaf', '3')])]),
73+
u('parent', [u('leaf', '2'), u('parent', [u('leaf', '3')])]),
3474
u('leaf', '4')
3575
])
3676

@@ -45,57 +85,69 @@ Yields:
4585
{
4686
type: 'root',
4787
children: [
48-
{type: 'node', children: [{type: 'leaf', value: '2'}]},
88+
{type: 'parent', children: [{type: 'leaf', value: '2'}]},
4989
{type: 'leaf', value: '4'}
5090
]
5191
}
5292
```
5393

5494
## API
5595

56-
This package exports the following identifiers: `filter`.
96+
This package exports the identifier `filter`.
5797
There is no default export.
5898

5999
### `filter(tree[, options][, test])`
60100

61-
Create a new [tree][] consisting of copies of all nodes that pass `test`.
101+
Create a new `tree` ([`Node`][node]) of copies of all nodes that pass `test`.
102+
(`Test` from [`unist-util-is`][test]).
62103
The tree is walked in [preorder][] (NLR), visiting the node itself, then its
63-
[head][], etc.
104+
head, etc.
64105

65-
###### Parameters
106+
##### `options`
66107

67-
* `tree` ([`Node?`][node])
68-
[Tree][] to filter
69-
* `options.cascade` (`boolean`, default: `true`)
70-
— Whether to drop parent nodes if they had children, but all their children
71-
were filtered out
72-
* `test` ([`Test`][is], optional) — [`is`][is]-compatible test (such as a
73-
[type][])
108+
Configuration (optional).
109+
110+
###### `options.cascade`
111+
112+
Whether to drop parent nodes if they had children, but all their children were
113+
filtered out (`boolean`, default: `true`).
74114

75115
###### Returns
76116

77-
[`Node?`][node] — New filtered [tree][].
117+
New filtered tree ([`Node?`][node]).
78118
`null` is returned if `tree` itself didn’t pass the test, or is cascaded away.
79119

120+
## Types
121+
122+
This package is fully typed with [TypeScript][].
123+
It exports the additional type `Options`.
124+
125+
## Compatibility
126+
127+
Projects maintained by the unified collective are compatible with all maintained
128+
versions of Node.js.
129+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
130+
Our projects sometimes work with older versions, but this is not guaranteed.
131+
80132
## Related
81133

82134
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
83-
Recursively walk over nodes
135+
— walk the tree
84136
* [`unist-util-visit-parents`](https://github.com/syntax-tree/unist-util-visit-parents)
85-
Like `visit`, but with a stack of parents
137+
walk the tree with a stack of parents
86138
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
87-
Create a new tree with all nodes mapped by a given function
139+
create a new tree with all nodes mapped by a given function
88140
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
89-
Create a new tree by mapping (to an array) by a given function
141+
create a new tree by mapping (to an array) by a given function
90142
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)
91-
Remove nodes from a tree that pass a test
143+
remove nodes from a tree that pass a test
92144
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
93-
Select nodes with CSS-like selectors
145+
select nodes with CSS-like selectors
94146

95147
## Contribute
96148

97-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
98-
started.
149+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
150+
ways to get started.
99151
See [`support.md`][support] for ways to get help.
100152

101153
This project has a [code of conduct][coc].
@@ -136,24 +188,32 @@ abide by its terms.
136188

137189
[npm]: https://docs.npmjs.com/cli/install
138190

191+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
192+
193+
[esmsh]: https://esm.sh
194+
195+
[typescript]: https://www.typescriptlang.org
196+
139197
[license]: license
140198

141-
[unist]: https://github.com/syntax-tree/unist
199+
[health]: https://github.com/syntax-tree/.github
142200

143-
[node]: https://github.com/syntax-tree/unist#node
201+
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
202+
203+
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
144204

145-
[tree]: https://github.com/syntax-tree/unist#tree
205+
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
146206

147-
[preorder]: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
207+
[unist]: https://github.com/syntax-tree/unist
148208

149-
[head]: https://github.com/syntax-tree/unist#head
209+
[node]: https://github.com/syntax-tree/unist#node
150210

151-
[type]: https://github.com/syntax-tree/unist#type
211+
[preorder]: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
152212

153-
[is]: https://github.com/syntax-tree/unist-util-is
213+
[unist-util-remove]: https://github.com/syntax-tree/unist-util-remove
154214

155-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
215+
[unist-util-visit]: https://github.com/syntax-tree/unist-util-visit
156216

157-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
217+
[unist-builder]: https://github.com/syntax-tree/unist-builder
158218

159-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
219+
[test]: https://github.com/syntax-tree/unist-util-is#test

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ test('should not cascade-remove nodes that were empty initially', (t) => {
7272

7373
test('should call iterator with `index` and `parent` args', (t) => {
7474
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
75-
/** @type {Array.<[Node, number|null|undefined, Parent|null|undefined]>} */
75+
/** @type {Array<[Node, number|null|undefined, Parent|null|undefined]>} */
7676
const callLog = []
7777

7878
t.deepEqual(

0 commit comments

Comments
 (0)