|
8 | 8 | [![Backers][backers-badge]][collective]
|
9 | 9 | [![Chat][chat-badge]][chat]
|
10 | 10 |
|
11 |
| -[**unist**][unist] utility to assert trees. |
| 11 | +[unist][] utility to assert trees. |
12 | 12 |
|
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 | + * [`assert(node[, parent])`](#assertnode-parent) |
| 21 | + * [`parent(node[, parent])`](#parentnode-parent) |
| 22 | + * [`literal(node[, parent])`](#literalnode-parent) |
| 23 | + * [`_void(node[, parent])`](#_voidnode-parent) |
| 24 | + * [`wrap(fn)`](#wrapfn) |
| 25 | +* [Extensions](#extensions) |
| 26 | +* [Types](#types) |
| 27 | +* [Compatibility](#compatibility) |
| 28 | +* [Related](#related) |
| 29 | +* [Contribute](#contribute) |
| 30 | +* [License](#license) |
| 31 | + |
| 32 | +## What is this? |
| 33 | + |
| 34 | +This package is a tiny utility that helps you deal with nodes. |
| 35 | + |
| 36 | +## When should I use this? |
14 | 37 |
|
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 | +This utility is typically useful when you expect certain nodes in your APIs |
| 39 | +and want to make sure they’re valid and as expected. |
17 | 40 |
|
18 |
| -[npm][]: |
| 41 | +Different utilities, [`mdast-util-assert`][mdast-util-assert], |
| 42 | +[`hast-util-assert`][hast-util-assert], and [`nlcst-test`][nlcst-test], |
| 43 | +do the same but for mdast, hast, and nlcst nodes, respectively. |
| 44 | + |
| 45 | +## Install |
| 46 | + |
| 47 | +This package is [ESM only][esm]. |
| 48 | +In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]: |
19 | 49 |
|
20 | 50 | ```sh
|
21 | 51 | npm install unist-util-assert
|
22 | 52 | ```
|
23 | 53 |
|
| 54 | +In Deno with [`esm.sh`][esmsh]: |
| 55 | + |
| 56 | +```js |
| 57 | +import {assert} from 'https://esm.sh/unist-util-assert@3' |
| 58 | +``` |
| 59 | + |
| 60 | +In browsers with [`esm.sh`][esmsh]: |
| 61 | + |
| 62 | +```html |
| 63 | +<script type="module"> |
| 64 | + import {assert} from 'https://esm.sh/unist-util-assert@3?bundle' |
| 65 | +</script> |
| 66 | +``` |
| 67 | + |
24 | 68 | ## Use
|
25 | 69 |
|
26 | 70 | ```js
|
@@ -49,57 +93,74 @@ assert({type: 'paragraph', children: ['foo']})
|
49 | 93 |
|
50 | 94 | ## API
|
51 | 95 |
|
52 |
| -This package exports the following identifiers: `assert`, `parent`, `literal`, |
53 |
| -`_void`, `wrap`. |
| 96 | +This package exports the identifiers `assert`, `parent`, `literal`, `_void`, |
| 97 | +and `wrap`. |
54 | 98 | There is no default export.
|
55 | 99 |
|
56 | 100 | ### `assert(node[, parent])`
|
57 | 101 |
|
58 |
| -Assert that `node` is a valid [unist][] [node][]. |
59 |
| -If `node` is a [parent][], all [child][]ren will be asserted too. |
| 102 | +Assert that `node` is a valid unist [`Node`][node]. |
| 103 | +If `tree` is a [parent][], all children will be asserted as well. |
| 104 | + |
| 105 | +###### Throws |
| 106 | + |
| 107 | +When `node`, or one of its children, is not a valid node. |
60 | 108 |
|
61 | 109 | ### `parent(node[, parent])`
|
62 | 110 |
|
63 |
| -Assert that `node` is a valid [unist][] [parent][]. |
64 |
| -All [child][]ren will be asserted too. |
| 111 | +Assert that `node` is a valid unist [`Parent`][parent]. |
| 112 | +All children will be asserted as well. |
65 | 113 |
|
66 | 114 | ### `literal(node[, parent])`
|
67 | 115 |
|
68 |
| -Assert that `node` is a valid [unist][] [literal][]. |
| 116 | +Assert that `node` is a valid unist [`Literal`][literal]. |
69 | 117 |
|
70 | 118 | ### `_void(node[, parent])`
|
71 | 119 |
|
72 |
| -Assert that `node` is a valid [unist][] [node][], but neither [parent][] nor |
73 |
| -[literal][]. |
74 |
| - |
75 |
| -## Extensions |
76 |
| - |
77 |
| -This module can be used as a base to test subsets of [unist][] (for an example, |
78 |
| -see [`mdast-util-assert`][mdast-util-assert]). |
79 |
| -All functions that are exposed from such a module, and functions used internally |
80 |
| -to test [child][]ren, should be wrapped in `wrap`, which adds an inspectable |
81 |
| -string of the respective node, and its parent when available, to the exposed |
82 |
| -error message. |
| 120 | +Assert that `node` is a valid unist [`Node`][node], but neither |
| 121 | +[`Parent`][parent] nor |
| 122 | +[`Literal`][literal]. |
83 | 123 |
|
84 | 124 | ### `wrap(fn)`
|
85 | 125 |
|
86 | 126 | Wraps `fn` (which is passed a node, and an optional parent node), so that any
|
87 | 127 | errors thrown inside it will contain information regarding the node (and the
|
88 | 128 | parent, when given).
|
89 | 129 |
|
| 130 | +## Extensions |
| 131 | + |
| 132 | +This module can be used as a base to test subsets of unist (for an example, see |
| 133 | +[`mdast-util-assert`][mdast-util-assert]). |
| 134 | +All functions that are exposed from such a module, and functions used internally |
| 135 | +to test children, should be wrapped in `wrap`, which adds an inspectable string |
| 136 | +of the respective node, and its parent when available, to the exposed error |
| 137 | +message. |
| 138 | + |
| 139 | +## Types |
| 140 | + |
| 141 | +This package is fully typed with [TypeScript][]. |
| 142 | +It does not export additional types. |
| 143 | + |
| 144 | +## Compatibility |
| 145 | + |
| 146 | +Projects maintained by the unified collective are compatible with all maintained |
| 147 | +versions of Node.js. |
| 148 | +As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. |
| 149 | +Our projects sometimes work with older versions, but this is not guaranteed. |
| 150 | + |
90 | 151 | ## Related
|
91 | 152 |
|
92 | 153 | * [`mdast-util-assert`][mdast-util-assert]
|
93 |
| - — Check [mdast](https://github.com/syntax-tree/mdast) nodes |
| 154 | + — assert mdast nodes |
94 | 155 | * [`hast-util-assert`](https://github.com/syntax-tree/hast-util-assert)
|
95 |
| - — Check [hast](https://github.com/syntax-tree/hast) nodes |
| 156 | + — assert hast nodes |
96 | 157 | * [`nlcst-test`](https://github.com/syntax-tree/nlcst-test)
|
97 |
| - — Check [nlcst](https://github.com/syntax-tree/nlcst) nodes |
| 158 | + — assert nlcst nodes |
98 | 159 |
|
99 | 160 | ## Contribute
|
100 | 161 |
|
101 |
| -See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get |
102 |
| -started. |
| 162 | +See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for |
| 163 | +ways to get started. |
103 | 164 | See [`support.md`][support] for ways to get help.
|
104 | 165 |
|
105 | 166 | This project has a [code of conduct][coc].
|
@@ -140,24 +201,34 @@ abide by its terms.
|
140 | 201 |
|
141 | 202 | [npm]: https://docs.npmjs.com/cli/install
|
142 | 203 |
|
| 204 | +[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c |
| 205 | + |
| 206 | +[esmsh]: https://esm.sh |
| 207 | + |
| 208 | +[typescript]: https://www.typescriptlang.org |
| 209 | + |
143 | 210 | [license]: license
|
144 | 211 |
|
145 | 212 | [author]: https://wooorm.com
|
146 | 213 |
|
147 |
| -[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md |
| 214 | +[health]: https://github.com/syntax-tree/.github |
148 | 215 |
|
149 |
| -[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md |
| 216 | +[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md |
150 | 217 |
|
151 |
| -[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md |
| 218 | +[support]: https://github.com/syntax-tree/.github/blob/main/support.md |
| 219 | + |
| 220 | +[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md |
152 | 221 |
|
153 | 222 | [unist]: https://github.com/syntax-tree/unist
|
154 | 223 |
|
| 224 | +[node]: https://github.com/syntax-tree/unist#node |
| 225 | + |
155 | 226 | [parent]: https://github.com/syntax-tree/unist#parent
|
156 | 227 |
|
157 | 228 | [literal]: https://github.com/syntax-tree/unist#literal
|
158 | 229 |
|
159 |
| -[node]: https://github.com/syntax-tree/unist#node |
| 230 | +[mdast-util-assert]: https://github.com/syntax-tree/mdast-util-assert |
160 | 231 |
|
161 |
| -[child]: https://github.com/syntax-tree/unist#child |
| 232 | +[hast-util-assert]: https://github.com/syntax-tree/hast-util-assert |
162 | 233 |
|
163 |
| -[mdast-util-assert]: https://github.com/syntax-tree/mdast-util-assert |
| 234 | +[nlcst-test]: https://github.com/syntax-tree/nlcst-test |
0 commit comments