Skip to content

Commit 9ab7e7f

Browse files
committed
Add improved docs
1 parent 7ed530b commit 9ab7e7f

File tree

1 file changed

+60
-27
lines changed

1 file changed

+60
-27
lines changed

readme.md

+60-27
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* [Use](#use)
1919
* [API](#api)
2020
* [`Index(prop|keyFunction[, tree[, test]])`](#indexpropkeyfunction-tree-test)
21+
* [`KeyFunction`](#keyfunction)
22+
* [`Test`](#test)
2123
* [Types](#types)
2224
* [Compatibility](#compatibility)
2325
* [Related](#related)
@@ -41,7 +43,7 @@ wrapper makes it all a bit easier.
4143
## Install
4244

4345
This package is [ESM only][esm].
44-
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
46+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
4547

4648
```sh
4749
npm install unist-util-index
@@ -50,14 +52,14 @@ npm install unist-util-index
5052
In Deno with [`esm.sh`][esmsh]:
5153

5254
```js
53-
import {Index} from "https://esm.sh/unist-util-index@3"
55+
import {Index} from 'https://esm.sh/unist-util-index@3'
5456
```
5557

5658
In browsers with [`esm.sh`][esmsh]:
5759

5860
```html
5961
<script type="module">
60-
import {Index} from "https://esm.sh/unist-util-index@3?bundle"
62+
import {Index} from 'https://esm.sh/unist-util-index@3?bundle'
6163
</script>
6264
```
6365

@@ -75,12 +77,12 @@ const tree = fromMarkdown(await fs.readFile('readme.md'))
7577
// Index on heading depth:
7678
const indexOnDepth = new Index('depth', tree, 'heading')
7779

78-
console.log(indexOnDepth.get(2).map(toString))
80+
console.log(indexOnDepth.get(2).map((d) => toString(d)))
7981

8082
// Index on definition identifier:
8183
const indexOnIdentifier = new Index('identifier', tree, 'definition')
8284

83-
console.log(indexOnIdentifier.get('unist').map(node => node.url))
85+
console.log(indexOnIdentifier.get('unist').map((node) => node.url))
8486
```
8587

8688
Yields:
@@ -104,70 +106,97 @@ Yields:
104106

105107
## API
106108

107-
This package exports the identifier `Index`.
109+
This package exports the identifier [`Index`][index].
108110
There is no default export.
109111

110112
### `Index(prop|keyFunction[, tree[, test]])`
111113

112114
Create a mutable index data structure, that maps property values or computed
113115
keys, to nodes.
114116

115-
If `tree` is given, the index is initialized with all nodes, optionally filtered
116-
by `test`.
117+
If `tree` is given, the index is initialized with all nodes, optionally
118+
filtered by `test`.
117119

118120
###### Parameters
119121

120122
* `prop` (`string`)
121123
— field to look up in each node to find keys
122-
* `keyFunction` ([`KeyFunction`][key-function])
124+
* `keyFunction` ([`KeyFunction`][keyfunction])
123125
— function called with each node to calculate keys
124126
* `tree` ([`Node`][node], optional)
125127
— tree to index
126-
* `test` ([`Test`][is], optional)
127-
[`is`][is]-compatible test
128+
* `test` ([`Test`][test], optional)
129+
`unist-util-is` compatible test
128130

129131
###### Returns
130132

131133
Instance (`Index`).
132134

133-
#### `function keyFunction(node)`
134-
135-
Function called with every added node ([`Node`][node]) to calculate the key to
136-
index on.
137-
138-
###### Returns
135+
#### `Index#get(key)`
139136

140-
Key to index on (`unknown`).
141-
Can be anything that can be used as a key in a [`Map`][map].
137+
Get nodes by `key`.
142138

143-
#### `Index#get(key)`
139+
###### Parameters
144140

145-
Get nodes by `key` (`unknown`).
141+
* `key` (`unknown`)
142+
— key to retrieve, can be anything that can be used as a key in a
143+
[`Map`][map]
146144

147145
###### Returns
148146

149147
List of zero or more nodes ([`Array<Node>`][node]).
150148

151149
#### `Index#add(node)`
152150

153-
Add `node` ([`Node`][node]) to the index (if not already present).
151+
Add `node` to the index (if not already present).
152+
153+
###### Parameters
154+
155+
* `node` ([`Node`][node])
156+
— node to index
154157

155158
###### Returns
156159

157160
Current instance (`Index`).
158161

159162
#### `Index#remove(node)`
160163

161-
Remove `node` ([`Node`][node]) from the index (if present).
164+
Remove `node` from the index (if present).
165+
166+
###### Parameters
167+
168+
* `node` ([`Node`][node])
169+
— node to remove
162170

163171
###### Returns
164172

165173
Current instance (`Index`).
166174

175+
### `KeyFunction`
176+
177+
Function called with every added node to calculate the key to index on
178+
(TypeScript type).
179+
180+
###### Parameters
181+
182+
* `node` ([`Node`][node])
183+
— node to calculate a key for
184+
185+
###### Returns
186+
187+
Key to index on (`unknown`).
188+
189+
Can be anything that can be used as a key in a [`Map`][map].
190+
191+
### `Test`
192+
193+
[`unist-util-is`][unist-util-is] compatible test (TypeScript type).
194+
167195
## Types
168196

169197
This package is fully typed with [TypeScript][].
170-
It exports the additional types `KeyFunction` and `Test`.
198+
It exports the additional types [`KeyFunction`][keyfunction] and
199+
[`Test`][test].
171200

172201
## Compatibility
173202

@@ -249,8 +278,12 @@ abide by its terms.
249278

250279
[node]: https://github.com/syntax-tree/unist#node
251280

252-
[is]: https://github.com/syntax-tree/unist-util-is
253-
254281
[map]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map
255282

256-
[key-function]: #function-keyfunctionnode
283+
[unist-util-is]: https://github.com/syntax-tree/unist-util-is
284+
285+
[index]: #indexpropkeyfunction-tree-test
286+
287+
[keyfunction]: #keyfunction
288+
289+
[test]: #test

0 commit comments

Comments
 (0)