|
1 |
| -[](https://npmjs.com/package/unist-util-index) |
| 1 | +# unist-util-index [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] |
2 | 2 |
|
3 |
| -# unist-util-index |
| 3 | +Create mutable index mapping property values or computed keys back to |
| 4 | +[**unist**][unist] nodes. |
4 | 5 |
|
5 |
| -[![Build Status][travis-badge]][travis] [![Dependency Status][david-badge]][david] |
| 6 | +## Installation |
6 | 7 |
|
7 |
| -Create mutable index mapping property values or computed keys back to [Unist] nodes. |
| 8 | +[npm][]: |
8 | 9 |
|
9 |
| -[unist]: https://github.com/wooorm/unist |
| 10 | +```bash |
| 11 | +npm install unist-util-index |
| 12 | +``` |
10 | 13 |
|
11 |
| -[travis]: https://travis-ci.org/eush77/unist-util-index |
12 |
| -[travis-badge]: https://travis-ci.org/eush77/unist-util-index.svg?branch=master |
13 |
| -[david]: https://david-dm.org/eush77/unist-util-index |
14 |
| -[david-badge]: https://david-dm.org/eush77/unist-util-index.png |
| 14 | +## Usage |
15 | 15 |
|
16 |
| -## Example |
| 16 | +## API |
17 | 17 |
|
18 |
| -Headings by depth: |
| 18 | +### `Index([tree[, filter]], prop|keyFn)` |
19 | 19 |
|
20 |
| -```js |
21 |
| -var Index = require('unist-util-index'), |
22 |
| - fs = require('fs'), |
23 |
| - remark = require('remark'), |
24 |
| - toString = require('mdast-util-to-string'); |
| 20 | +Create an index data structure that maps keys (calculated by `keyFn` function |
| 21 | +or the values at `prop` in each node) to a list of nodes. |
25 | 22 |
|
26 |
| -var ast = remark.parse(fs.readFileSync('README.md', 'utf8')); |
27 |
| -var index = Index(ast, 'heading', 'depth'); |
| 23 | +If `tree` is given, the index is initialised with all nodes, optionally |
| 24 | +filtered by `filter`. |
28 | 25 |
|
29 |
| -index.get(1).map(toString) |
30 |
| -//=> [ 'unist-util-index' ] |
| 26 | +###### Signatures |
31 | 27 |
|
32 |
| -index.get(2).map(toString) |
33 |
| -//=> [ 'Example', 'API', 'Install', 'License' ] |
34 |
| -``` |
| 28 | +* `Index(prop|keyFn)` |
| 29 | +* `Index(tree, prop|keyFn)` |
| 30 | +* `Index(tree, filter, prop|keyFn)` |
35 | 31 |
|
36 |
| -Definitions by identifier: |
| 32 | +###### Parameters |
37 | 33 |
|
38 |
| -```js |
39 |
| -var index = Index(ast, 'definition', 'identifier'); |
| 34 | +* `tree` ([`Node`][node]) |
| 35 | +* `filter` (`*`) — [`is`][is]-compatible test |
| 36 | +* `prop` (`string`) — Property to look up in each node to find keys |
| 37 | +* `keyFn` ([`Function`][keyfn]) — Function called with each node to calculate |
| 38 | + keys |
40 | 39 |
|
41 |
| -index.get('unist') |
42 |
| -//=> [ { type: 'definition', |
43 |
| -// identifier: 'unist', |
44 |
| -// title: null, |
45 |
| -// url: 'https://github.com/wooorm/unist', |
46 |
| -// position: Position { start: [Object], end: [Object], indent: [] } } ] |
| 40 | +###### Returns |
47 | 41 |
|
48 |
| -index.get('travis') |
49 |
| -//=> [ { type: 'definition', |
50 |
| -// identifier: 'travis', |
51 |
| -// title: null, |
52 |
| -// url: 'https://travis-ci.org/eush77/unist-util-index', |
53 |
| -// position: Position { start: [Object], end: [Object], indent: [] } } ] |
54 |
| -``` |
| 42 | +`Index` — an index instance. |
55 | 43 |
|
56 |
| -## API |
| 44 | +#### `function keyFn(node)` |
57 | 45 |
|
58 |
| -### `index = Index([ast, [filter]], key)` |
| 46 | +Function called with every added [node][] to return the value to index on. |
59 | 47 |
|
60 |
| -- `ast` — [Unist] tree. |
| 48 | +#### `Index#get(key)` |
61 | 49 |
|
62 |
| -- `filter` — one of: |
63 |
| - - node type (string); |
64 |
| - - function invoked with arguments `(node, index?, parent?)`. |
| 50 | +Get nodes by `key` (`*`). |
| 51 | +Returns a list of zero or more nodes ([`Array.<Node>`][node]). |
65 | 52 |
|
66 |
| -- `key` — one of: |
67 |
| - - property name (string); |
68 |
| - - function invoked with argument `(node)`. |
| 53 | +#### `Index#add(node)` |
69 | 54 |
|
70 |
| -Create index data structure that maps keys (returned by `key` function or property) to nodes. |
| 55 | +Add [`node`][node] to the index (if not already present). |
71 | 56 |
|
72 |
| -If `ast` argument is given, initialize index with `ast` nodes (recursively), optionally filter by node type or predicate. |
| 57 | +#### `Index#remove(node)` |
73 | 58 |
|
74 |
| -### `index.get(key)` |
| 59 | +Remove [`node`][node] from the index (if present). |
75 | 60 |
|
76 |
| -- `key` — index key. |
| 61 | +## Related |
77 | 62 |
|
78 |
| -Get nodes by `key`. |
| 63 | +* [`unist-util-is`](https://github.com/syntax-tree/unist-util-is) |
| 64 | + — Utility to check if a node passes a test |
| 65 | +* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit) |
| 66 | + — Utility to recursively walk over nodes |
| 67 | +* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map) |
| 68 | + — Create a new tree by mapping by the provided function |
| 69 | +* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap) |
| 70 | + — Create a new tree by mapping and then flattening |
| 71 | +* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select) |
| 72 | + — Select nodes with CSS-like selectors |
79 | 73 |
|
80 |
| -Returns array of nodes. |
| 74 | +## Contribute |
81 | 75 |
|
82 |
| -### `index.add(node)` |
| 76 | +See [`contributing.md` in `syntax-tree/unist`][contributing] for ways to get |
| 77 | +started. |
83 | 78 |
|
84 |
| -- `node` — [Unist] node. |
| 79 | +This organisation has a [Code of Conduct][coc]. By interacting with this |
| 80 | +repository, organisation, or community you agree to abide by its terms. |
85 | 81 |
|
86 |
| -Add `node` to index. No-op if `node` is already present in index. |
| 82 | +## License |
87 | 83 |
|
88 |
| -Returns `index`. |
| 84 | +[MIT][license] © Eugene Sharygin |
89 | 85 |
|
90 |
| -### `index.remove(node)` |
| 86 | +<!-- Definitions --> |
91 | 87 |
|
92 |
| -- `node` — [Unist] node. |
| 88 | +[travis-badge]: https://img.shields.io/travis/syntax-tree/unist-util-index.svg |
93 | 89 |
|
94 |
| -Remove `node` from index. No-op if `node` is not present in index. |
| 90 | +[travis]: https://travis-ci.org/syntax-tree/unist-util-index |
95 | 91 |
|
96 |
| -Returns `index`. |
| 92 | +[codecov-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-index.svg |
97 | 93 |
|
98 |
| -## Install |
| 94 | +[codecov]: https://codecov.io/github/syntax-tree/unist-util-indexs |
99 | 95 |
|
100 |
| -``` |
101 |
| -npm install unist-util-index |
102 |
| -``` |
| 96 | +[npm]: https://docs.npmjs.com/cli/install |
103 | 97 |
|
104 |
| -## License |
| 98 | +[license]: license |
| 99 | + |
| 100 | +[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md |
| 101 | + |
| 102 | +[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md |
| 103 | + |
| 104 | +[unist]: https://github.com/syntax-tree/unist |
| 105 | + |
| 106 | +[node]: https://github.com/syntax-tree/unist#node |
| 107 | + |
| 108 | +[is]: https://github.com/syntax-tree/unist-util-is |
105 | 109 |
|
106 |
| -MIT |
| 110 | +[keyfn]: #function-keyfnnode |
0 commit comments