Skip to content

Commit ff08753

Browse files
committed
Add improved docs
1 parent 820e2c8 commit ff08753

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
22
* @typedef {import('./lib/index.js').Options} Options
3+
*
34
* @typedef {Options} FilterOptions
45
* Deprecated, use `Options`.
56
*/
7+
// To do: next major: remove `FilterOptions`.
68

79
export {filter} from './lib/index.js'

lib/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ const own = {}.hasOwnProperty
1616

1717
/**
1818
* Create a new `tree` of copies of all nodes that pass `test`.
19-
* The tree is walked in preorder (NLR), visiting the node itself, then its
19+
*
20+
* The tree is walked in *preorder* (NLR), visiting the node itself, then its
2021
* head, etc.
2122
*
2223
* @param tree
2324
* Tree to filter.
2425
* @param options
2526
* Configuration (optional).
2627
* @param test
27-
* `unist-util-is`-compatible test (such as a type).
28+
* `unist-util-is` compatible test.
2829
* @returns
2930
* New filtered tree.
3031
*
@@ -34,7 +35,7 @@ const own = {}.hasOwnProperty
3435
export const filter =
3536
/**
3637
* @type {(
37-
* (<Tree extends Node, Check extends Test>(node: Tree, options: Options | null | undefined, test: Check) => import('./complex-types.js').Matches<Tree, Check>) &
38+
* (<Tree extends Node, Check extends Test>(node: Tree, options: Options | null | undefined, test: Check | null | undefined) => import('./complex-types.js').Matches<Tree, Check>) &
3839
* (<Tree extends Node, Check extends Test>(node: Tree, test: Check) => import('./complex-types.js').Matches<Tree, Check>) &
3940
* (<Tree extends Node>(node: Tree, options?: Options | null | undefined) => Tree)
4041
* )}

readme.md

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [Use](#use)
1919
* [API](#api)
2020
* [`filter(tree[, options][, test])`](#filtertree-options-test)
21+
* [`Options`](#options)
2122
* [Types](#types)
2223
* [Compatibility](#compatibility)
2324
* [Related](#related)
@@ -42,7 +43,7 @@ To create trees, use [`unist-builder`][unist-builder].
4243
## Install
4344

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

4748
```sh
4849
npm install unist-util-filter
@@ -51,14 +52,14 @@ npm install unist-util-filter
5152
In Deno with [`esm.sh`][esmsh]:
5253

5354
```js
54-
import {filter} from "https://esm.sh/unist-util-filter@4"
55+
import {filter} from 'https://esm.sh/unist-util-filter@4'
5556
```
5657

5758
In browsers with [`esm.sh`][esmsh]:
5859

5960
```html
6061
<script type="module">
61-
import {filter} from "https://esm.sh/unist-util-filter@4?bundle"
62+
import {filter} from 'https://esm.sh/unist-util-filter@4?bundle'
6263
</script>
6364
```
6465

@@ -93,40 +94,51 @@ Yields:
9394

9495
## API
9596

96-
This package exports the identifier `filter`.
97+
This package exports the identifier [`filter`][filter].
9798
There is no default export.
9899

99100
### `filter(tree[, options][, test])`
100101

101-
Create a new `tree` ([`Node`][node]) of copies of all nodes that pass `test`.
102-
(`Test` from [`unist-util-is`][test]).
103-
The tree is walked in [preorder][] (NLR), visiting the node itself, then its
104-
head, etc.
105-
106-
##### `options`
102+
Create a new `tree` of copies of all nodes that pass `test`.
107103

108-
Configuration (optional).
104+
The tree is walked in *[preorder][]* (NLR), visiting the node itself, then its
105+
head, etc.
109106

110-
###### `options.cascade`
107+
###### Parameters
111108

112-
Whether to drop parent nodes if they had children, but all their children were
113-
filtered out (`boolean`, default: `true`).
109+
* `tree` ([`Node`][node])
110+
— tree to filter
111+
* `options` ([`Options`][options], optional)
112+
— configuration
113+
* `test` ([`Test`][test], optional)
114+
`unist-util-is` compatible test
114115

115116
###### Returns
116117

117-
New filtered tree ([`Node?`][node]).
118+
New filtered tree ([`Node`][node] or `null`).
119+
118120
`null` is returned if `tree` itself didn’t pass the test, or is cascaded away.
119121

122+
### `Options`
123+
124+
Configuration (TypeScript type).
125+
126+
###### Fields
127+
128+
* `cascade` (`boolean`, default: `true`)
129+
— whether to drop parent nodes if they had children, but all their
130+
children were filtered out
131+
120132
## Types
121133

122134
This package is fully typed with [TypeScript][].
123-
It exports the additional type `Options`.
135+
It exports the additional type [`Options`][options].
124136

125137
## Compatibility
126138

127139
Projects maintained by the unified collective are compatible with all maintained
128140
versions of Node.js.
129-
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
141+
As of now, that is Node.js 14.14+ and 16.0+.
130142
Our projects sometimes work with older versions, but this is not guaranteed.
131143

132144
## Related
@@ -217,3 +229,7 @@ abide by its terms.
217229
[unist-builder]: https://github.com/syntax-tree/unist-builder
218230

219231
[test]: https://github.com/syntax-tree/unist-util-is#test
232+
233+
[filter]: #filtertree-options-test
234+
235+
[options]: #options

0 commit comments

Comments
 (0)