Skip to content

Commit d262197

Browse files
committed
Add improved docs
1 parent 5dd9f26 commit d262197

File tree

1 file changed

+90
-44
lines changed

1 file changed

+90
-44
lines changed

readme.md

+90-44
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,56 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**unist**][unist] utility to find a node before another node.
11+
[unist][] utility to find a node before another node.
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+
* [`findBefore(parent, node|index[, test])`](#findbeforeparent-nodeindex-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 tiny utility that you can use to find a node before another node or
30+
before an index in a parent.
1431

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.
32+
## When should I use this?
33+
34+
This is super tiny.
35+
You can of course do it yourself.
36+
But this helps when integrating with the rest of unified and unist.
37+
38+
## Install
1739

18-
[npm][]:
40+
This package is [ESM only][esm].
41+
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
1942

2043
```sh
2144
npm install unist-util-find-before
2245
```
2346

47+
In Deno with [`esm.sh`][esmsh]:
48+
49+
```js
50+
import {findBefore} from "https://esm.sh/unist-util-find-before@3"
51+
```
52+
53+
In browsers with [`esm.sh`][esmsh]:
54+
55+
```html
56+
<script type="module">
57+
import {findBefore} from "https://esm.sh/unist-util-find-before@3?bundle"
58+
</script>
59+
```
60+
2461
## Use
2562

2663
```js
@@ -29,76 +66,81 @@ import {findBefore} from 'unist-util-find-before'
2966

3067
const tree = u('tree', [
3168
u('leaf', 'leaf 1'),
32-
u('node', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
69+
u('parent', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
3370
u('leaf', 'leaf 4'),
34-
u('node', [u('leaf', 'leaf 5')]),
71+
u('parent', [u('leaf', 'leaf 5')]),
3572
u('leaf', 'leaf 6'),
36-
u('void'),
73+
u('empty'),
3774
u('leaf', 'leaf 7')
3875
])
3976

4077
const empty = tree.children[5]
4178

42-
console.log(findBefore(tree, empty, 'node'))
79+
console.log(findBefore(tree, empty, 'parent'))
4380
```
4481

4582
Yields:
4683

4784
```js
48-
{type: 'node', children: [{type: 'leaf', value: 'leaf 5'}]}
85+
{type: 'parent', children: [{type: 'leaf', value: 'leaf 5'}]}
4986
```
5087

5188
## API
5289

53-
This package exports the following identifiers: `findBefore`.
90+
This package exports the identifier `findBefore`.
5491
There is no default export.
5592

5693
### `findBefore(parent, node|index[, test])`
5794

58-
Find the first [child][] before `index` (or `node`) in `parent`, that passes
59-
`test`.
95+
Find the first node in `parent` ([`Parent`][parent]) before another node
96+
([`Node`][node]) or before an index, that passes `test` (`Test` from
97+
[`unist-util-is`][test]).
6098

61-
###### Parameters
99+
###### Returns
62100

63-
* `parent` ([`Node`][node]) — [Parent][] node
64-
* `node` ([`Node`][node]) — [Child][] of `parent`
65-
* `index` (`number`, optional) — [Index][] in `parent`
66-
* `test` (`Function`, `string`, `Object`, `Array`, optional)
67-
— See [`unist-util-is`][is]
101+
Child of `parent` that passes `test`, if found ([`Node?`][node]).
68102

69-
###### Returns
103+
## Types
104+
105+
This package is fully typed with [TypeScript][].
106+
It exports no additional types (types for the test are in `unist-util-is`).
107+
108+
## Compatibility
70109

71-
[`Node?`][node][Child][] of `parent` passing `test`.
110+
Projects maintained by the unified collective are compatible with all maintained
111+
versions of Node.js.
112+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
113+
Our projects sometimes work with older versions, but this is not guaranteed.
72114

73115
## Related
74116

75117
* [`unist-util-find-after`](https://github.com/syntax-tree/unist-util-find-after)
76-
Find a node after another node
118+
find a node after another node
77119
* [`unist-util-find-all-after`](https://github.com/syntax-tree/unist-util-find-all-after)
78-
Find all nodes after another node
120+
find all nodes after another node
79121
* [`unist-util-find-all-before`](https://github.com/syntax-tree/unist-util-find-all-before)
80-
Find all nodes before another node
122+
find all nodes before another node
81123
* [`unist-util-find-all-between`](https://github.com/mrzmmr/unist-util-find-all-between)
82-
Find all nodes between two nodes
124+
find all nodes between two nodes
83125
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
84-
Recursively walk over nodes
126+
— walk the tree
85127
* [`unist-util-visit-parents`](https://github.com/syntax-tree/unist-util-visit-parents)
86-
Like `visit`, but with a stack of parents
128+
walk the tree with a stack of parents
87129
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)
88-
Create a new tree with all nodes that pass a test
130+
create a new tree with all nodes that pass a test
89131
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
90-
Create a new tree with all nodes mapped by a given function
132+
create a new tree with all nodes mapped by a given function
91133
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
92-
Create a new tree by mapping (to an array) by a given function
134+
create a new tree by mapping (to an array) by a given function
93135
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)
94-
Remove nodes from a tree that pass a test
136+
remove nodes from a tree that pass a test
95137
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
96-
Select nodes with CSS-like selectors
138+
select nodes with CSS-like selectors
97139

98140
## Contribute
99141

100-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
101-
started.
142+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
143+
ways to get started.
102144
See [`support.md`][support] for ways to get help.
103145

104146
This project has a [Code of Conduct][coc].
@@ -139,24 +181,28 @@ abide by its terms.
139181

140182
[npm]: https://docs.npmjs.com/cli/install
141183

184+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
185+
186+
[esmsh]: https://esm.sh
187+
188+
[typescript]: https://www.typescriptlang.org
189+
142190
[license]: license
143191

144192
[author]: https://wooorm.com
145193

146-
[unist]: https://github.com/syntax-tree/unist
147-
148-
[node]: https://github.com/syntax-tree/unist#node
194+
[health]: https://github.com/syntax-tree/.github
149195

150-
[parent]: https://github.com/syntax-tree/unist#parent-1
196+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
151197

152-
[child]: https://github.com/syntax-tree/unist#child
198+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
153199

154-
[index]: https://github.com/syntax-tree/unist#index
200+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
155201

156-
[is]: https://github.com/syntax-tree/unist-util-is
202+
[unist]: https://github.com/syntax-tree/unist
157203

158-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
204+
[node]: https://github.com/syntax-tree/unist#node
159205

160-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
206+
[parent]: https://github.com/syntax-tree/unist#parent-1
161207

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

0 commit comments

Comments
 (0)