|
1 | | -# unist-util-find-before [](https://travis-ci.org/wooorm/unist-util-find-before) [](https://codecov.io/github/wooorm/unist-util-find-before?branch=master) |
| 1 | +# unist-util-find-before [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] |
2 | 2 |
|
3 | | -[**Unist**](https://github.com/wooorm/unist) utility to find a node before |
4 | | -another node. Useful when working with [**mdast**](https://github.com/wooorm/mdast) |
5 | | -or [**retext**](https://github.com/wooorm/retext). |
| 3 | +[**Unist**][unist] utility to find a node before another node. |
6 | 4 |
|
7 | 5 | ## Installation |
8 | 6 |
|
9 | | -[npm](https://docs.npmjs.com/cli/install): |
| 7 | +[npm][]: |
10 | 8 |
|
11 | 9 | ```bash |
12 | 10 | npm install unist-util-find-before |
13 | 11 | ``` |
14 | 12 |
|
15 | | -**unist-util-find-before** is also available for [bower](http://bower.io/#install-packages), |
16 | | -[component](https://github.com/componentjs/component), and |
17 | | -[duo](http://duojs.org/#getting-started), and as an AMD, CommonJS, and globals |
18 | | -module, [uncompressed](unist-util-find-before.js) and |
19 | | -[compressed](unist-util-find-before.min.js). |
20 | | - |
21 | 13 | ## Usage |
22 | 14 |
|
23 | 15 | ```js |
24 | | -var mdast = require('mdast'); |
| 16 | +var remark = require('remark'); |
25 | 17 | var findBefore = require('unist-util-find-before'); |
26 | | -var inspect = require('unist-util-inspect'); |
27 | | - |
28 | | -function log(node) { |
29 | | - console.log(node && inspect(node)); |
30 | | -} |
31 | | - |
32 | | -mdast.use(function () { |
33 | | - return function (ast) { |
34 | | - var paragraph = ast.children[0]; |
35 | | - var children = paragraph.children; |
36 | | - |
37 | | - log(findBefore(paragraph, 4)); |
38 | | - log(findBefore(paragraph, children[4])); |
39 | | - log(findBefore(paragraph, children[4], 'emphasis')); |
40 | | - log(findBefore(paragraph, children[4], children[5])); |
41 | | - log(findBefore(paragraph, children[4], function (node, n) { |
42 | | - return n === 1; |
43 | | - })); |
44 | | - }; |
45 | | -}).process('Some *emphasis*, **strongness**, and `code`.'); |
| 18 | + |
| 19 | +var tree = remark().parse('Some _emphasis_, **importance**, and `code`.'); |
| 20 | +var paragraph = tree.children[0]; |
| 21 | +var code = paragraph.children[paragraph.children.length - 1]; |
| 22 | + |
| 23 | +console.log(findBefore(paragraph, code, 'emphasis')); |
46 | 24 | ``` |
47 | 25 |
|
48 | 26 | Yields: |
49 | 27 |
|
50 | | -```text |
51 | | -strong[1] |
52 | | -└─ text: 'strongness' |
53 | | -strong[1] |
54 | | -└─ text: 'strongness' |
55 | | -emphasis[1] |
56 | | -└─ text: 'emphasis' |
57 | | -null |
58 | | -emphasis[1] |
59 | | -└─ text: 'emphasis' |
| 28 | +```js |
| 29 | +{ type: 'emphasis', |
| 30 | + children: [ { type: 'text', value: 'emphasis' } ] } |
60 | 31 | ``` |
61 | 32 |
|
62 | 33 | ## API |
63 | 34 |
|
64 | | -### findBefore(parent, index|node\[, test\]) |
| 35 | +### `findBefore(parent, node|index[, test])` |
65 | 36 |
|
66 | | -Find the first child before `index` (or `node`), that passes `test` (when |
67 | | -given). |
| 37 | +Find the first child before `index` (or `node`) in `parent`, that passes `test` |
| 38 | +(when given). |
68 | 39 |
|
69 | | -**Parameters**: |
| 40 | +###### Parameters |
70 | 41 |
|
71 | | -* `parent` (`Node`) — Parent to search in; |
| 42 | +* `parent` ([`Node`][node]) — Context node; |
| 43 | +* `node` ([`Node`][node]) — Node in `parent`; |
| 44 | +* `index` (`number`, optional) — Position of a `node` in `parent`; |
| 45 | +* `test` (`Function`, `string`, or `Node`, optional) |
| 46 | + — See [`unist-util-is`][is]. |
72 | 47 |
|
73 | | -* `node` (`Node`) |
74 | | - — [Node](https://github.com/wooorm/unist#unist-nodes) to search before; |
| 48 | +###### Returns |
75 | 49 |
|
76 | | -* `index` (`number`) — Position of child to search before; |
| 50 | +[`Node?`][node] — Child node of `parent` passing `test`. |
77 | 51 |
|
78 | | -* `test` (`Function`, `string`, or `Node`; optional) |
79 | | - — See [`is()`](https://github.com/wooorm/unist-util-is#istest-node-index-parent-context). |
| 52 | +## License |
80 | 53 |
|
81 | | -**Returns**: `node?`, when found. Child node of `parent` which passes `test`. |
| 54 | +[MIT][license] © [Titus Wormer][author] |
82 | 55 |
|
83 | | -## License |
| 56 | +<!-- Definitions --> |
| 57 | + |
| 58 | +[travis-badge]: https://img.shields.io/travis/wooorm/unist-util-find-before.svg |
| 59 | + |
| 60 | +[travis]: https://travis-ci.org/wooorm/unist-util-find-before |
| 61 | + |
| 62 | +[codecov-badge]: https://img.shields.io/codecov/c/github/wooorm/unist-util-find-before.svg |
| 63 | + |
| 64 | +[codecov]: https://codecov.io/github/wooorm/unist-util-find-before |
| 65 | + |
| 66 | +[npm]: https://docs.npmjs.com/cli/install |
| 67 | + |
| 68 | +[license]: LICENSE |
| 69 | + |
| 70 | +[author]: http://wooorm.com |
| 71 | + |
| 72 | +[unist]: https://github.com/wooorm/unist |
| 73 | + |
| 74 | +[node]: https://github.com/wooorm/unist#node |
84 | 75 |
|
85 | | -[MIT](LICENSE) © [Titus Wormer](http://wooorm.com) |
| 76 | +[is]: https://github.com/wooorm/unist-util-is |
0 commit comments