Skip to content

Commit 200f653

Browse files
committed
Add improved docs
1 parent e5b9d66 commit 200f653

File tree

2 files changed

+104
-42
lines changed

2 files changed

+104
-42
lines changed

example.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {u} from 'unist-builder'
2+
import {findAllBefore} from './index.js'
3+
4+
const tree = u('tree', [
5+
u('leaf', 'leaf 1'),
6+
u('parent', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
7+
u('leaf', 'leaf 4'),
8+
u('parent', [u('leaf', 'leaf 5')]),
9+
u('leaf', 'leaf 6'),
10+
u('empty'),
11+
u('leaf', 'leaf 7')
12+
])
13+
14+
const leaf6 = tree.children[4]
15+
16+
console.log(findAllBefore(tree, leaf6, 'leaf'))

readme.md

Lines changed: 88 additions & 42 deletions
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 nodes before another node.
11+
[unist][] utility to find nodes 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+
* [`findAllBefore(parent, node|index[, test])`](#findallbeforeparent-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 nodes before another node or
30+
before an index in a parent.
31+
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.
1437

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.
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-all-before
2245
```
2346

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

2663
```js
@@ -29,11 +66,11 @@ import {findAllBefore} from 'unist-util-find-all-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

@@ -46,62 +83,67 @@ Yields:
4683

4784
```js
4885
[
49-
{ type: 'leaf', value: 'leaf 4' },
50-
{ type: 'leaf', value: 'leaf 1' }
86+
{ type: 'leaf', value: 'leaf 1' },
87+
{ type: 'leaf', value: 'leaf 4' }
5188
]
5289
```
5390

5491
## API
5592

56-
This package exports the following identifiers: `findAllBefore`.
93+
This package exports the identifier `findAllBefore`.
5794
There is no default export.
5895

5996
### `findAllBefore(parent, node|index[, test])`
6097

61-
Find the first child before `index` (or `node`) in `parent`, that passes `test`
62-
(when given).
98+
Find the nodes in `parent` ([`Parent`][parent]) before another `node`
99+
([`Node`][node]) or before an index, that pass `test` (`Test` from
100+
[`unist-util-is`][test]).
101+
102+
###### Returns
63103

64-
###### Parameters
104+
Children of `parent` that pass `test` ([`Array<Node>`][node]).
65105

66-
* `parent` ([`Node`][node]) — [Parent][] node
67-
* `node` ([`Node`][node]) — [Child][] of `parent`
68-
* `index` (`number`, optional) — [Index][] in `parent`
69-
* `test` (`Function`, `string`, `Object`, `Array`, optional)
70-
— See [`unist-util-is`][is]
106+
## Types
71107

72-
###### Returns
108+
This package is fully typed with [TypeScript][].
109+
It exports no additional types (types for the test are in `unist-util-is`).
110+
111+
## Compatibility
73112

74-
[`Array.<Node>`][node][Child][]ren of `parent` passing `test`.
113+
Projects maintained by the unified collective are compatible with all maintained
114+
versions of Node.js.
115+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
116+
Our projects sometimes work with older versions, but this is not guaranteed.
75117

76118
## Related
77119

78120
* [`unist-util-find-after`](https://github.com/syntax-tree/unist-util-find-after)
79-
Find a node after another node
121+
find a node after another node
80122
* [`unist-util-find-before`](https://github.com/syntax-tree/unist-util-find-before)
81-
Find a node before another node
123+
find a node before another node
82124
* [`unist-util-find-all-after`](https://github.com/syntax-tree/unist-util-find-all-after)
83-
Find all nodes after another node
125+
find all nodes after another node
84126
* [`unist-util-find-all-between`](https://github.com/mrzmmr/unist-util-find-all-between)
85-
Find all nodes between two nodes
127+
find all nodes between two nodes
86128
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
87-
Recursively walk over nodes
129+
— walk the tree
88130
* [`unist-util-visit-parents`](https://github.com/syntax-tree/unist-util-visit-parents)
89-
Like `visit`, but with a stack of parents
131+
walk the tree with a stack of parents
90132
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)
91-
Create a new tree with all nodes that pass a test
133+
create a new tree with all nodes that pass a test
92134
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
93-
Create a new tree with all nodes mapped by a given function
135+
create a new tree with all nodes mapped by a given function
94136
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
95-
Create a new tree by mapping (to an array) with a given function
137+
create a new tree by mapping (to an array) with a given function
96138
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)
97-
Remove nodes from a tree that pass a test
139+
remove nodes from a tree that pass a test
98140
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
99-
Select nodes with CSS-like selectors
141+
select nodes with CSS-like selectors
100142

101143
## Contribute
102144

103-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
104-
started.
145+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
146+
ways to get started.
105147
See [`support.md`][support] for ways to get help.
106148

107149
This project has a [Code of Conduct][coc].
@@ -142,24 +184,28 @@ abide by its terms.
142184

143185
[npm]: https://docs.npmjs.com/cli/install
144186

187+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
188+
189+
[esmsh]: https://esm.sh
190+
191+
[typescript]: https://www.typescriptlang.org
192+
145193
[license]: license
146194

147195
[author]: https://wooorm.com
148196

149-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
197+
[health]: https://github.com/syntax-tree/.github
150198

151-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
199+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
152200

153-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
201+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
202+
203+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
154204

155205
[unist]: https://github.com/syntax-tree/unist
156206

157207
[node]: https://github.com/syntax-tree/unist#node
158208

159209
[parent]: https://github.com/syntax-tree/unist#parent-1
160210

161-
[child]: https://github.com/syntax-tree/unist#child
162-
163-
[index]: https://github.com/syntax-tree/unist#index
164-
165-
[is]: https://github.com/syntax-tree/unist-util-is
211+
[test]: https://github.com/syntax-tree/unist-util-is#test

0 commit comments

Comments
 (0)