|
1 |
| -# unist-util-find [](https://travis-ci.org/blahah/unist-util-find) |
| 1 | +# unist-util-find |
2 | 2 |
|
3 |
| -[Unist](https://github.com/wooorm/unist) node finder utility. Useful for working with [remark](https://github.com/wooorm/remark), [rehype](https://github.com/wooorm/rehype) and [retext](https://github.com/wooorm/retext). |
| 3 | +[![Build][build-badge]][build] |
| 4 | +[![Coverage][coverage-badge]][coverage] |
| 5 | +[![Downloads][downloads-badge]][downloads] |
| 6 | +[![Size][size-badge]][size] |
| 7 | +[![Sponsors][sponsors-badge]][collective] |
| 8 | +[![Backers][backers-badge]][collective] |
| 9 | +[![Chat][chat-badge]][chat] |
4 | 10 |
|
5 |
| -## Installation |
| 11 | +[unist][] utility to find a node. |
6 | 12 |
|
| 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 | + * [`find(tree, condition)`](#findtree-condition) |
| 21 | + * [`TestFn`](#testfn) |
| 22 | + * [`TestObj`](#testobj) |
| 23 | + * [`TestStr`](#teststr) |
| 24 | +* [Types](#types) |
| 25 | +* [Compatibility](#compatibility) |
| 26 | +* [Security](#security) |
| 27 | +* [Related](#related) |
| 28 | +* [Contribute](#contribute) |
| 29 | +* [License](#license) |
| 30 | + |
| 31 | +## What is this? |
| 32 | + |
| 33 | +This package is a utility that takes any [unist][] (whether mdast, hast, etc) |
| 34 | +node and returns the first node that matches a given condition. |
| 35 | + |
| 36 | +## When should I use this? |
| 37 | + |
| 38 | +This utility is the simplest way to find a single node in a tree. |
| 39 | + |
| 40 | +For much more powerful tree walking, see [`unist-util-visit`][visit]. |
| 41 | + |
| 42 | +## Install |
| 43 | + |
| 44 | +This package is [ESM only][esm]. |
| 45 | +In Node.js (version 16+), install with [npm][]: |
| 46 | + |
| 47 | +```sh |
| 48 | +npm install unist-util-find |
7 | 49 | ```
|
8 |
| -npm install --save unist-util-find |
| 50 | + |
| 51 | +In Deno with [`esm.sh`][esmsh]: |
| 52 | + |
| 53 | +```js |
| 54 | +import {find} from 'https://esm.sh/unist-util-find@1' |
9 | 55 | ```
|
10 | 56 |
|
11 |
| -## Usage |
| 57 | +In browsers with [`esm.sh`][esmsh]: |
12 | 58 |
|
13 |
| -### Example |
| 59 | +```html |
| 60 | +<script type="module"> |
| 61 | + import {find} from 'https://esm.sh/unist-util-find@1?bundle' |
| 62 | +</script> |
| 63 | +``` |
| 64 | + |
| 65 | +## Use |
14 | 66 |
|
15 | 67 | ```js
|
16 | 68 | import {fromMarkdown} from 'mdast-util-from-markdown'
|
17 |
| -import {find} from './index.js' |
| 69 | +import {find} from 'unist-util-find' |
18 | 70 |
|
19 | 71 | const tree = fromMarkdown('Some _emphasis_, **strongness**, and `code`.')
|
20 | 72 |
|
21 |
| -// String condition |
| 73 | +// String condition: |
22 | 74 | console.log(find(tree, 'value'))
|
23 | 75 |
|
24 |
| -// Object condition |
| 76 | +// Object condition: |
25 | 77 | console.log(find(tree, {value: 'emphasis'}))
|
26 | 78 |
|
27 |
| -// Function condition |
| 79 | +// Function condition: |
28 | 80 | console.log(
|
29 | 81 | find(tree, function (node) {
|
30 | 82 | return node.type === 'inlineCode'
|
31 | 83 | })
|
32 | 84 | )
|
33 | 85 | ```
|
34 | 86 |
|
35 |
| -Result: |
| 87 | +Yields: |
36 | 88 |
|
37 |
| -``` |
| 89 | +```js |
38 | 90 | // string condition: 'value'
|
39 | 91 | {
|
40 | 92 | type: 'text',
|
@@ -66,18 +118,164 @@ Result:
|
66 | 118 | }
|
67 | 119 | ```
|
68 | 120 |
|
69 |
| -### API |
| 121 | +## API |
| 122 | + |
| 123 | +This package exports the identifier [`find`][api-find]. |
| 124 | +There is no default export. |
| 125 | + |
| 126 | +### `find(tree, condition)` |
| 127 | + |
| 128 | +Find a node in `tree` matching `condition`. |
| 129 | + |
| 130 | +###### Parameters |
| 131 | + |
| 132 | +* `tree` ([`Node`][node]) |
| 133 | + — tree to search in |
| 134 | +* `condition` ([`TestFn`][api-test-fn], [`TestObj`][api-test-obj], or |
| 135 | + [`TestStr`][api-test-str]) |
| 136 | + — condition used to test each node |
| 137 | + |
| 138 | +###### Returns |
| 139 | + |
| 140 | +The first node ([`Node`][node]) that matches condition, or `undefined` if no |
| 141 | +node matches |
| 142 | + |
| 143 | +### `TestFn` |
| 144 | + |
| 145 | +Find the first node for which function returns `true` when passed node as |
| 146 | +argument (TypeScript type). |
| 147 | + |
| 148 | +###### Parameters |
| 149 | + |
| 150 | +* `node` ([`Node`][node]) |
| 151 | + — node to check |
| 152 | + |
| 153 | +###### Returns |
| 154 | + |
| 155 | +Whether `node` matches your condition (`boolean`). |
| 156 | + |
| 157 | +### `TestObj` |
| 158 | + |
| 159 | +Find the first node that has matching values for all properties of object |
| 160 | +(TypeScript type). |
| 161 | + |
| 162 | +###### Type |
| 163 | + |
| 164 | +```ts |
| 165 | +type TestObj = Record<string, unknown>; |
| 166 | +``` |
| 167 | + |
| 168 | +### `TestStr` |
| 169 | + |
| 170 | +Find the first node with a truthy property matching `string` (TypeScript type). |
| 171 | + |
| 172 | +###### Type |
| 173 | + |
| 174 | +```ts |
| 175 | +type TestStr = string; |
| 176 | +``` |
| 177 | + |
| 178 | +## Types |
| 179 | + |
| 180 | +This package is fully typed with [TypeScript][]. |
| 181 | +It exports the additional types [`TestFn`][api-test-fn], |
| 182 | +[`TestObj`][api-test-obj], and [`TestStr`][api-test-str]. |
| 183 | + |
| 184 | +## Compatibility |
| 185 | + |
| 186 | +Projects maintained by the unified collective are compatible with maintained |
| 187 | +versions of Node.js. |
| 188 | + |
| 189 | +When we cut a new major release, we drop support for unmaintained versions of |
| 190 | +Node. |
| 191 | +This means we try to keep the current release line, |
| 192 | +`unist-util-find@^1`, compatible with Node.js 12. |
70 | 193 |
|
71 |
| -#### `find(node, condition)` |
| 194 | +## Security |
72 | 195 |
|
73 |
| -Return the first node that matches `condition`, or `undefined` if no node matches. |
| 196 | +This project is safe. |
74 | 197 |
|
75 |
| -- `node` ([`Node`](https://github.com/wooorm/unist#node)) - Node to search |
76 |
| -- `condition` (`string`, `object` or `function`) - Condition used to test each node. Behaviour depends on the type of the condition: |
77 |
| - - `string` finds first node with a truthy property matching `string` |
78 |
| - - `object` finds first node that has matching values for all properties of `object` |
79 |
| - - `function` finds first node for which `function` returns true when passed `node` as argument |
| 198 | +## Related |
| 199 | + |
| 200 | +* [`unist-util-generated`](https://github.com/syntax-tree/unist-util-generated) |
| 201 | + — check if a node is generated |
| 202 | +* [`unist-util-position`](https://github.com/syntax-tree/unist-util-position) |
| 203 | + — get positional info of nodes |
| 204 | +* [`unist-util-remove-position`](https://github.com/syntax-tree/unist-util-remove-position) |
| 205 | + — remove positional info from trees |
| 206 | +* [`unist-util-source`](https://github.com/syntax-tree/unist-util-source) |
| 207 | + — get the source of a value (node or position) in a file |
| 208 | + |
| 209 | +## Contribute |
| 210 | + |
| 211 | +See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get |
| 212 | +started. |
| 213 | +See [`support.md`][support] for ways to get help. |
| 214 | + |
| 215 | +This project has a [code of conduct][coc]. |
| 216 | +By interacting with this repository, organization, or community you agree to |
| 217 | +abide by its terms. |
80 | 218 |
|
81 | 219 | ## License
|
82 | 220 |
|
83 |
| -MIT |
| 221 | +[MIT][license] © [Titus Wormer][author] |
| 222 | + |
| 223 | +<!-- Definition --> |
| 224 | + |
| 225 | +[build-badge]: https://github.com/syntax-tree/unist-util-find/workflows/main/badge.svg |
| 226 | + |
| 227 | +[build]: https://github.com/syntax-tree/unist-util-find/actions |
| 228 | + |
| 229 | +[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-find.svg |
| 230 | + |
| 231 | +[coverage]: https://codecov.io/github/syntax-tree/unist-util-find |
| 232 | + |
| 233 | +[downloads-badge]: https://img.shields.io/npm/dm/unist-util-find.svg |
| 234 | + |
| 235 | +[downloads]: https://www.npmjs.com/package/unist-util-find |
| 236 | + |
| 237 | +[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-util-find |
| 238 | + |
| 239 | +[size]: https://bundlejs.com/?q=unist-util-find |
| 240 | + |
| 241 | +[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg |
| 242 | + |
| 243 | +[backers-badge]: https://opencollective.com/unified/backers/badge.svg |
| 244 | + |
| 245 | +[collective]: https://opencollective.com/unified |
| 246 | + |
| 247 | +[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg |
| 248 | + |
| 249 | +[chat]: https://github.com/syntax-tree/unist/discussions |
| 250 | + |
| 251 | +[npm]: https://docs.npmjs.com/cli/install |
| 252 | + |
| 253 | +[license]: license |
| 254 | + |
| 255 | +[author]: https://wooorm.com |
| 256 | + |
| 257 | +[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c |
| 258 | + |
| 259 | +[esmsh]: https://esm.sh |
| 260 | + |
| 261 | +[typescript]: https://www.typescriptlang.org |
| 262 | + |
| 263 | +[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md |
| 264 | + |
| 265 | +[support]: https://github.com/syntax-tree/.github/blob/main/support.md |
| 266 | + |
| 267 | +[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md |
| 268 | + |
| 269 | +[unist]: https://github.com/syntax-tree/unist |
| 270 | + |
| 271 | +[visit]: https://github.com/syntax-tree/unist-util-visit |
| 272 | + |
| 273 | +[node]: https://github.com/syntax-tree/unist#node |
| 274 | + |
| 275 | +[api-find]: #findtree-condition |
| 276 | + |
| 277 | +[api-test-fn]: #testfn |
| 278 | + |
| 279 | +[api-test-obj]: #testobj |
| 280 | + |
| 281 | +[api-test-str]: #teststr |
0 commit comments