Skip to content

Commit

Permalink
Add improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 3, 2022
1 parent 5dd9f26 commit d262197
Showing 1 changed file with 90 additions and 44 deletions.
134 changes: 90 additions & 44 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,56 @@
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

[**unist**][unist] utility to find a node before another node.
[unist][] utility to find a node before another node.

## Install
## Contents

* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`findBefore(parent, node|index[, test])`](#findbeforeparent-nodeindex-test)
* [Types](#types)
* [Compatibility](#compatibility)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)

## What is this?

This is a tiny utility that you can use to find a node before another node or
before an index in a parent.

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
## When should I use this?

This is super tiny.
You can of course do it yourself.
But this helps when integrating with the rest of unified and unist.

## Install

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

```sh
npm install unist-util-find-before
```

In Deno with [`esm.sh`][esmsh]:

```js
import {findBefore} from "https://esm.sh/unist-util-find-before@3"
```

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

```html
<script type="module">
import {findBefore} from "https://esm.sh/unist-util-find-before@3?bundle"
</script>
```

## Use

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

const tree = u('tree', [
u('leaf', 'leaf 1'),
u('node', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
u('parent', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
u('leaf', 'leaf 4'),
u('node', [u('leaf', 'leaf 5')]),
u('parent', [u('leaf', 'leaf 5')]),
u('leaf', 'leaf 6'),
u('void'),
u('empty'),
u('leaf', 'leaf 7')
])

const empty = tree.children[5]

console.log(findBefore(tree, empty, 'node'))
console.log(findBefore(tree, empty, 'parent'))
```

Yields:

```js
{type: 'node', children: [{type: 'leaf', value: 'leaf 5'}]}
{type: 'parent', children: [{type: 'leaf', value: 'leaf 5'}]}
```

## API

This package exports the following identifiers: `findBefore`.
This package exports the identifier `findBefore`.
There is no default export.

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

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

###### Parameters
###### Returns

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

###### Returns
## Types

This package is fully typed with [TypeScript][].
It exports no additional types (types for the test are in `unist-util-is`).

## Compatibility

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

## Related

* [`unist-util-find-after`](https://github.com/syntax-tree/unist-util-find-after)
Find a node after another node
find a node after another node
* [`unist-util-find-all-after`](https://github.com/syntax-tree/unist-util-find-all-after)
Find all nodes after another node
find all nodes after another node
* [`unist-util-find-all-before`](https://github.com/syntax-tree/unist-util-find-all-before)
Find all nodes before another node
find all nodes before another node
* [`unist-util-find-all-between`](https://github.com/mrzmmr/unist-util-find-all-between)
Find all nodes between two nodes
find all nodes between two nodes
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
Recursively walk over nodes
— walk the tree
* [`unist-util-visit-parents`](https://github.com/syntax-tree/unist-util-visit-parents)
Like `visit`, but with a stack of parents
walk the tree with a stack of parents
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)
Create a new tree with all nodes that pass a test
create a new tree with all nodes that pass a test
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
Create a new tree with all nodes mapped by a given function
create a new tree with all nodes mapped by a given function
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
Create a new tree by mapping (to an array) by a given function
create a new tree by mapping (to an array) by a given function
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)
Remove nodes from a tree that pass a test
remove nodes from a tree that pass a test
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
Select nodes with CSS-like selectors
select nodes with CSS-like selectors

## Contribute

See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
started.
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
ways to get started.
See [`support.md`][support] for ways to get help.

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

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

[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

[esmsh]: https://esm.sh

[typescript]: https://www.typescriptlang.org

[license]: license

[author]: https://wooorm.com

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

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

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

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

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

[is]: https://github.com/syntax-tree/unist-util-is
[unist]: https://github.com/syntax-tree/unist

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

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

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

0 comments on commit d262197

Please sign in to comment.