Skip to content

Commit ccb5949

Browse files
committed
Add improved docs
1 parent b7ffc07 commit ccb5949

File tree

1 file changed

+140
-51
lines changed

1 file changed

+140
-51
lines changed

readme.md

Lines changed: 140 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,63 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**unist**][unist] utility to check if a node passes a test.
11+
[unist][] utility to check if nodes pass a test.
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+
* [`is(node[, test[, index, parent[, context]]])`](#isnode-test-index-parent-context)
21+
* [`convert(test)`](#converttest)
22+
* [Examples](#examples)
23+
* [Example of `convert`](#example-of-convert)
24+
* [Types](#types)
25+
* [Compatibility](#compatibility)
26+
* [Related](#related)
27+
* [Contribute](#contribute)
28+
* [License](#license)
29+
30+
## What is this?
31+
32+
This package is a small utility that checks that a node is a certain node.
33+
34+
## When should I use this?
35+
36+
Use this small utility if you find yourself repeating code for checking what
37+
nodes are.
1438

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.
39+
A similar package, [`hast-util-is-element`][hast-util-is-element], works on hast
40+
elements.
1741

18-
[npm][]:
42+
For more advanced tests, [`unist-util-select`][unist-util-select] can be used
43+
to match against CSS selectors.
44+
45+
## Install
46+
47+
This package is [ESM only][esm].
48+
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
1949

2050
```sh
2151
npm install unist-util-is
2252
```
2353

54+
In Deno with [`esm.sh`][esmsh]:
55+
56+
```js
57+
import {is} from "https://esm.sh/unist-util-is@5"
58+
```
59+
60+
In browsers with [`esm.sh`][esmsh]:
61+
62+
```html
63+
<script type="module">
64+
import {is} from "https://esm.sh/unist-util-is@5?bundle"
65+
</script>
66+
```
67+
2468
## Use
2569

2670
```js
@@ -29,10 +73,6 @@ import {is} from 'unist-util-is'
2973
const node = {type: 'strong'}
3074
const parent = {type: 'paragraph', children: [node]}
3175

32-
function test(node, n) {
33-
return n === 5
34-
}
35-
3676
is() // => false
3777
is({children: []}) // => false
3878
is(node) // => true
@@ -46,75 +86,89 @@ is(parent, {type: 'strong'}) // => false
4686
is(node, test) // => false
4787
is(node, test, 4, parent) // => false
4888
is(node, test, 5, parent) // => true
89+
90+
function test(node, n) {
91+
return n === 5
92+
}
4993
```
5094

5195
## API
5296

53-
This package exports the following identifiers: `is`, `convert`.
97+
This package exports the identifiers `is` and `convert`.
5498
There is no default export.
5599

56100
### `is(node[, test[, index, parent[, context]]])`
57101

102+
Check if `node` passes a test.
103+
When a `parent` node is given the `index` of node should also be given.
104+
58105
###### Parameters
59106

60-
* `node` ([`Node`][node]) — Node to check.
107+
* `node` ([`Node`][node]) — node to check
61108
* `test` ([`Function`][test], `string`, `Object`, or `Array<Test>`, optional)
62-
— When nullish, checks if `node` is a [`Node`][node].
63-
When `string`, works like passing `node => node.type === test`.
64-
When `array`, checks if any one of the subtests pass.
65-
When `object`, checks that all keys in `test` are in `node`,
66-
and that they have strictly equal values
67-
* `index` (`number`, optional) — [Index][] of `node` in `parent`
68-
* `parent` ([`Node`][node], optional) — [Parent][] of `node`
69-
* `context` (`*`, optional) — Context object to invoke `test` with
109+
Check.
110+
* when nullish, checks if `node` is a [`Node`][node]
111+
* when `string`, works like passing `node => node.type === test`
112+
* when `array`, checks if any one of the subtests pass
113+
* when `object`, checks that all fields in `test` are in `node` and that
114+
they have strictly equal values
115+
* when `function` checks if function passed the node is true
116+
* `index` (`number`, optional) — position of `node` in `parent`.
117+
* `parent` ([`Node`][node], optional) — parent of `node`
118+
* `context` (`*`, optional) — context object to call `test` with
70119

71120
###### Returns
72121

73-
`boolean` — Whether `test` passed *and* `node` is a [`Node`][node] (object with
74-
`type` set to a non-empty `string`).
122+
Whether `test` passed *and* `node` is a [`Node`][node] (`boolean`).
75123

76-
#### `function test(node[, index, parent])`
124+
#### `test(node[, index, parent])`
77125

78-
###### Parameters
79-
80-
* `node` ([`Node`][node]) — Node to check
81-
* `index` (`number?`) — [Index][] of `node` in `parent`
82-
* `parent` ([`Node?`][node]) — [Parent][] of `node`
126+
Arbitrary function to define whether a node passes.
83127

84-
###### Context
128+
###### Parameters
85129

86-
`*` — The to `is` given `context`.
130+
* `this` (`*`) — the to `is` given `context`.
131+
* `node` ([`Node`][node]) — node to check
132+
* `index` (`number?`) — [index][] of `node` in `parent`
133+
* `parent` ([`Node?`][node]) — [parent][] of `node`
87134

88135
###### Returns
89136

90-
`boolean?`Whether `node` matches.
137+
Whether `node` matches (`boolean?`).
91138

92139
### `convert(test)`
93140

94-
Create a test function from `test`, that can later be called with a `node`,
141+
Create a test function from `test` that can later be called with a `node`,
95142
`index`, and `parent`.
96143
Useful if you’re going to test many nodes, for example when creating a utility
97144
where something else passes an is-compatible test.
98145

99-
The created function is slightly faster because it expects valid input only.
100-
Therefore, passing invalid input, yields unexpected results.
146+
The created function is slightly faster that using `is` because it expects valid
147+
input only.
148+
Therefore, passing invalid input yields unexpected results.
101149

102-
For example:
150+
###### Returns
151+
152+
Check function that can be called as `check(node, index, parent)`.
153+
154+
## Examples
155+
156+
### Example of `convert`
103157

104158
```js
105-
import u from 'unist-builder'
159+
import {u} from 'unist-builder'
106160
import {convert} from 'unist-util-is'
107161

108-
var test = convert('leaf')
162+
const test = convert('leaf')
109163

110-
var tree = u('tree', [
164+
const tree = u('tree', [
111165
u('node', [u('leaf', '1')]),
112166
u('leaf', '2'),
113167
u('node', [u('leaf', '3'), u('leaf', '4')]),
114168
u('leaf', '5')
115169
])
116170

117-
var leafs = tree.children.filter((child, index) => test(child, index, tree))
171+
const leafs = tree.children.filter((child, index) => test(child, index, tree))
118172

119173
console.log(leafs)
120174
```
@@ -125,27 +179,50 @@ Yields:
125179
[{type: 'leaf', value: '2'}, {type: 'leaf', value: '5'}]
126180
```
127181

182+
## Types
183+
184+
This package is fully typed with [TypeScript][].
185+
It exports the additional types:
186+
187+
* `Test`
188+
— models any arbitrary test that can be given
189+
* `TestFunctionAnything`
190+
— models any test function
191+
* `TestFunctionPredicate<Kind>` (where `Kind` extends `Node`)
192+
— models a test function for `Kind`
193+
* `AssertAnything`
194+
— models a check function as returned by `convert`
195+
* `AssertPredicate<Kind>` (where `Kind` extends `Node`)
196+
— models a check function for `Kind` as returned by `convert`
197+
198+
## Compatibility
199+
200+
Projects maintained by the unified collective are compatible with all maintained
201+
versions of Node.js.
202+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
203+
Our projects sometimes work with older versions, but this is not guaranteed.
204+
128205
## Related
129206

130207
* [`unist-util-find-after`](https://github.com/syntax-tree/unist-util-find-after)
131-
Find a node after another node
208+
find a node after another node
132209
* [`unist-util-find-before`](https://github.com/syntax-tree/unist-util-find-before)
133-
Find a node before another node
210+
find a node before another node
134211
* [`unist-util-find-all-after`](https://github.com/syntax-tree/unist-util-find-all-after)
135-
Find all nodes after another node
212+
find all nodes after another node
136213
* [`unist-util-find-all-before`](https://github.com/syntax-tree/unist-util-find-all-before)
137-
Find all nodes before another node
214+
find all nodes before another node
138215
* [`unist-util-find-all-between`](https://github.com/mrzmmr/unist-util-find-all-between)
139-
Find all nodes between two nodes
216+
find all nodes between two nodes
140217
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)
141-
Create a new tree with nodes that pass a check
218+
create a new tree with nodes that pass a check
142219
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)
143-
Remove nodes from tree
220+
remove nodes from tree
144221

145222
## Contribute
146223

147-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
148-
started.
224+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
225+
ways to get started.
149226
See [`support.md`][support] for ways to get help.
150227

151228
This project has a [code of conduct][coc].
@@ -186,15 +263,23 @@ abide by its terms.
186263

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

266+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
267+
268+
[esmsh]: https://esm.sh
269+
270+
[typescript]: https://www.typescriptlang.org
271+
189272
[license]: license
190273

191274
[author]: https://wooorm.com
192275

193-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
276+
[health]: https://github.com/syntax-tree/.github
277+
278+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
194279

195-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
280+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
196281

197-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
282+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
198283

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

@@ -204,4 +289,8 @@ abide by its terms.
204289

205290
[index]: https://github.com/syntax-tree/unist#index
206291

207-
[test]: #function-testnode-index-parent
292+
[hast-util-is-element]: https://github.com/syntax-tree/hast-util-is-element
293+
294+
[unist-util-select]: https://github.com/syntax-tree/unist-util-select
295+
296+
[test]: #testnode-index-parent

0 commit comments

Comments
 (0)