Skip to content

Commit c26a78d

Browse files
committed
Add improved docs
1 parent 464afb6 commit c26a78d

File tree

2 files changed

+117
-42
lines changed

2 files changed

+117
-42
lines changed

index.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@
44
* @typedef {import('hast').Element} Element
55
*
66
* @typedef {string} TagName
7+
*
78
* @typedef {null|undefined|TagName|TestFunctionAnything|Array<TagName|TestFunctionAnything>} Test
8-
*/
9-
10-
/**
11-
* @template {Element} T
12-
* @typedef {null|undefined|T['tagName']|TestFunctionPredicate<T>|Array<T['tagName']|TestFunctionPredicate<T>>} PredicateTest
13-
*/
14-
15-
/**
16-
* Check if an element passes a test
179
*
1810
* @callback TestFunctionAnything
11+
* Check if an element passes a test.
1912
* @param {Element} element
2013
* @param {number|null|undefined} [index]
2114
* @param {Parent|null|undefined} [parent]
2215
* @returns {boolean|void}
2316
*/
2417

2518
/**
26-
* Check if an element passes a certain node test
19+
* @template {Element} T
20+
* @typedef {null|undefined|T['tagName']|TestFunctionPredicate<T>|Array<T['tagName']|TestFunctionPredicate<T>>} PredicateTest
21+
*/
22+
23+
/**
24+
* Check if an element passes a certain node test.
2725
*
2826
* @template {Element} X
2927
* @callback TestFunctionPredicate

readme.md

+109-32
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,61 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**hast**][hast] utility to check if a [*node*][node] is a (certain)
12-
[*element*][element].
11+
[hast][] utility to check if a node is a (certain) element.
1312

14-
## 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+
* [`isElement(node[, test[, index, parent[, context]]])`](#iselementnode-test-index-parent-context)
21+
* [`convertElement(test)`](#convertelementtest)
22+
* [Types](#types)
23+
* [Compatibility](#compatibility)
24+
* [Security](#security)
25+
* [Related](#related)
26+
* [Contribute](#contribute)
27+
* [License](#license)
28+
29+
## What is this?
30+
31+
This package is a small utility that checks that a node is a certain element.
32+
33+
## When should I use this?
34+
35+
Use this small utility if you find yourself repeating code for checking what
36+
elements nodes are.
37+
38+
A similar package, [`unist-util-is`][unist-util-is], works on any unist node.
39+
40+
For more advanced tests, [`hast-util-select`][hast-util-select] can be used
41+
to match against CSS selectors.
1542

16-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
17-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
43+
## Install
1844

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

2148
```sh
2249
npm install hast-util-is-element
2350
```
2451

52+
In Deno with [`esm.sh`][esmsh]:
53+
54+
```js
55+
import {isElement} from 'https://esm.sh/hast-util-is-element@2'
56+
```
57+
58+
In browsers with [`esm.sh`][esmsh]:
59+
60+
```html
61+
<script type="module">
62+
import {isElement} from 'https://esm.sh/hast-util-is-element@2?bundle'
63+
</script>
64+
```
65+
2566
## Use
2667

2768
```js
@@ -36,46 +77,45 @@ isElement({type: 'element', tagName: 'a'}, ['a', 'area']) // => true
3677

3778
## API
3879

39-
This package exports the following identifiers: `isElement`, `convertElement`.
80+
This package exports the identifiers `isElement` and `convertElement`.
4081
There is no default export.
4182

4283
### `isElement(node[, test[, index, parent[, context]]])`
4384

4485
Check if the given value is a (certain) [*element*][element].
4586

46-
* `node` ([`Node`][node]) — Node to check.
87+
###### Parameters
88+
89+
* `node` ([`Node`][node]) — node to check
4790
* `test` ([`Function`][test], `string`, or `Array<Test>`, optional)
48-
When `array`, checks if any one of the subtests pass.
91+
when `array`, checks if any one of the subtests pass.
4992
When `string`, checks that the element has that tag name.
5093
When `function`, see [`test`][test]
51-
* `index` (`number`, optional) — [Index][] of `node` in `parent`
52-
* `parent` ([`Node`][node], optional) — [Parent][] of `node`
53-
* `context` (`*`, optional) — Context object to invoke `test` with
94+
* `index` (`number`, optional) — [index][] of `node` in `parent`
95+
* `parent` ([`Node`][node], optional) — [parent][] of `node`
96+
* `context` (`*`, optional) — context object to call `test` with
5497

5598
###### Returns
5699

57-
`boolean`Whether `test` passed *and* `node` is an [`Element`][element].
100+
Whether `test` passed *and* `node` is an [`Element`][element] (`boolean`)
58101

59102
###### Throws
60103

61-
`Error`When an incorrect `test`, `index`, or `parent` is given.
104+
When an incorrect `test`, `index`, or `parent` is given.
62105
A `node` that is not a node, or not an element, does not throw.
63106

64107
#### `function test(element[, index, parent])`
65108

66109
###### Parameters
67110

68-
* `element` ([`Element`][element]) — Element to check
69-
* `index` (`number?`) — [Index][] of `node` in `parent`
70-
* `parent` ([`Node?`][node]) — [Parent][] of `node`
71-
72-
###### Context
73-
74-
`*` — The to `is` given `context`.
111+
* `this` — the to `is` given `context` (`*`)
112+
* `element` ([`Element`][element]) — element to check
113+
* `index` (`number?`) — [index][] of `node` in `parent`
114+
* `parent` ([`Node?`][node]) — [parent][] of `node`
75115

76116
###### Returns
77117

78-
`boolean?`Whether `element` matches.
118+
Whether `element` matches (`boolean?`).
79119

80120
### `convertElement(test)`
81121

@@ -87,6 +127,31 @@ where something else passes a compatible test.
87127
The created function is slightly faster because it expects valid input only.
88128
Therefore, passing invalid input, yields unexpected results.
89129

130+
## Types
131+
132+
This package is fully typed with [TypeScript][].
133+
It exports the additional types:
134+
135+
* `TestFunctionAnything`
136+
— models any test function
137+
* `TestFunctionPredicate<T>` (where `T` extends `Element`)
138+
— models a test function for `T`
139+
* `Test`
140+
— models any arbitrary test that can be given
141+
* `PredicateTest<T>` (where `T` extends `Element`)
142+
— models a test for `T`
143+
* `AssertAnything`
144+
— models a check function as returned by `convertElement`
145+
* `AssertPredicate<T>` (where `T` extends `Element`)
146+
— models a check function for `T` as returned by `convertElement`
147+
148+
## Compatibility
149+
150+
Projects maintained by the unified collective are compatible with all maintained
151+
versions of Node.js.
152+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
153+
Our projects sometimes work with older versions, but this is not guaranteed.
154+
90155
## Security
91156

92157
`hast-util-is-element` does not change the syntax tree so there are no openings
@@ -96,21 +161,21 @@ for [cross-site scripting (XSS)][xss] attacks.
96161

97162
* [`hast-util-has-property`](https://github.com/syntax-tree/hast-util-has-property)
98163
— check if a node has a property
99-
* [`hast-util-is-body-ok-link`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-body-ok-link)
164+
* [`hast-util-is-body-ok-link`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-body-ok-link)
100165
— check if a node is “Body OK” link element
101-
* [`hast-util-is-conditional-comment`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-conditional-comment)
166+
* [`hast-util-is-conditional-comment`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-conditional-comment)
102167
— check if a node is a conditional comment
103-
* [`hast-util-is-css-link`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-css-link)
168+
* [`hast-util-is-css-link`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-css-link)
104169
— check if a node is a CSS link element
105-
* [`hast-util-is-css-style`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-css-style)
170+
* [`hast-util-is-css-style`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-css-style)
106171
— check if a node is a CSS style element
107172
* [`hast-util-embedded`](https://github.com/syntax-tree/hast-util-embedded)
108173
— check if a node is an embedded element
109174
* [`hast-util-heading`](https://github.com/syntax-tree/hast-util-heading)
110175
— check if a node is a heading element
111176
* [`hast-util-interactive`](https://github.com/syntax-tree/hast-util-interactive)
112177
— check if a node is interactive
113-
* [`hast-util-is-javascript`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-javascript)
178+
* [`hast-util-is-javascript`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-javascript)
114179
— check if a node is a JavaScript script element
115180
* [`hast-util-labelable`](https://github.com/syntax-tree/hast-util-labelable)
116181
— check whether a node is labelable
@@ -127,8 +192,8 @@ for [cross-site scripting (XSS)][xss] attacks.
127192

128193
## Contribute
129194

130-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
131-
started.
195+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
196+
ways to get started.
132197
See [`support.md`][support] for ways to get help.
133198

134199
This project has a [code of conduct][coc].
@@ -169,15 +234,23 @@ abide by its terms.
169234

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

237+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
238+
239+
[esmsh]: https://esm.sh
240+
241+
[typescript]: https://www.typescriptlang.org
242+
172243
[license]: license
173244

174245
[author]: https://wooorm.com
175246

176-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
247+
[health]: https://github.com/syntax-tree/.github
177248

178-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
249+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
179250

180-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
251+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
252+
253+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
181254

182255
[hast]: https://github.com/syntax-tree/hast
183256

@@ -192,3 +265,7 @@ abide by its terms.
192265
[test]: #function-testelement-index-parent
193266

194267
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
268+
269+
[unist-util-is]: https://github.com/syntax-tree/unist-util-is
270+
271+
[hast-util-select]: https://github.com/syntax-tree/hast-util-select

0 commit comments

Comments
 (0)