Skip to content

Commit 580e76d

Browse files
committed
Add improved jsdoc
1 parent ffff195 commit 580e76d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

index.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,33 @@
22
* @typedef {import('unist').Node} Node
33
* @typedef {import('unist-util-visit').Test} Test
44
*
5-
* @typedef {(node: Node) => unknown} KeyFunction
5+
* @callback KeyFunction
6+
* Function called with every added node (`Node`) to calculate the key to
7+
* index on.
8+
* @param {Node} node
9+
* Node to calculate a key for.
10+
* @returns {unknown}
11+
* Key to index on.
12+
* Can be anything that can be used as a key in a `Map`.
613
*/
714

815
import {visit} from 'unist-util-visit'
916

1017
export class Index {
1118
/**
19+
* Create a mutable index data structure, that maps property values or
20+
* computed keys, to nodes.
21+
*
22+
* If `tree` is given, the index is initialized with all nodes, optionally
23+
* filtered by `test`.
24+
*
1225
* @param {string|KeyFunction} prop
26+
* Field (`string`) to look up in each node to find keys or function called
27+
* with each node to calculate keys.
1328
* @param {Node} [tree]
29+
* Tree to index.
1430
* @param {Test} [test]
31+
* `is`-compatible test.
1532
*/
1633
constructor(prop, tree, test) {
1734
/** @type {Map<unknown, Array<Node>>} */
@@ -28,15 +45,25 @@ export class Index {
2845
}
2946

3047
/**
48+
* Get nodes by `key`.
49+
*
3150
* @param {unknown} key
51+
* Key to retrieve.
52+
* Can be anything that can be used as a key in a `Map`.
3253
* @returns {Array<Node>}
54+
* List of zero or more nodes.
3355
*/
3456
get(key) {
3557
return this.index.get(key) || []
3658
}
3759

3860
/**
61+
* Add `node` to the index (if not already present).
62+
*
3963
* @param {Node} node
64+
* Node to index.
65+
* @returns
66+
* Current instance.
4067
*/
4168
add(node) {
4269
const key = this.key(node)
@@ -55,7 +82,12 @@ export class Index {
5582
}
5683

5784
/**
85+
* Remove `node` from the index (if present).
86+
*
5887
* @param {Node} node
88+
* Node to remove.
89+
* @returns
90+
* Current instance.
5991
*/
6092
remove(node) {
6193
const key = this.key(node)

readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ by `test`.
118118
###### Parameters
119119

120120
* `prop` (`string`)
121-
property to look up in each node to find keys
121+
field to look up in each node to find keys
122122
* `keyFunction` ([`KeyFunction`][key-function])
123123
— function called with each node to calculate keys
124124
* `tree` ([`Node`][node], optional)
@@ -154,15 +154,15 @@ Add `node` ([`Node`][node]) to the index (if not already present).
154154

155155
###### Returns
156156

157-
Nothing (`void`).
157+
Current instance (`Index`).
158158

159159
#### `Index#remove(node)`
160160

161161
Remove `node` ([`Node`][node]) from the index (if present).
162162

163163
###### Returns
164164

165-
Nothing (`void`).
165+
Current instance (`Index`).
166166

167167
## Types
168168

0 commit comments

Comments
 (0)