18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
20
* [ ` Index(prop|keyFunction[, tree[, test]]) ` ] ( #indexpropkeyfunction-tree-test )
21
+ * [ ` KeyFunction ` ] ( #keyfunction )
22
+ * [ ` Test ` ] ( #test )
21
23
* [ Types] ( #types )
22
24
* [ Compatibility] ( #compatibility )
23
25
* [ Related] ( #related )
@@ -41,7 +43,7 @@ wrapper makes it all a bit easier.
41
43
## Install
42
44
43
45
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] [ ] :
45
47
46
48
``` sh
47
49
npm install unist-util-index
@@ -50,14 +52,14 @@ npm install unist-util-index
50
52
In Deno with [ ` esm.sh ` ] [ esmsh ] :
51
53
52
54
``` js
53
- import {Index } from " https://esm.sh/unist-util-index@3"
55
+ import {Index } from ' https://esm.sh/unist-util-index@3'
54
56
```
55
57
56
58
In browsers with [ ` esm.sh ` ] [ esmsh ] :
57
59
58
60
``` html
59
61
<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'
61
63
</script >
62
64
```
63
65
@@ -75,12 +77,12 @@ const tree = fromMarkdown(await fs.readFile('readme.md'))
75
77
// Index on heading depth:
76
78
const indexOnDepth = new Index (' depth' , tree, ' heading' )
77
79
78
- console .log (indexOnDepth .get (2 ).map (toString))
80
+ console .log (indexOnDepth .get (2 ).map (( d ) => toString (d) ))
79
81
80
82
// Index on definition identifier:
81
83
const indexOnIdentifier = new Index (' identifier' , tree, ' definition' )
82
84
83
- console .log (indexOnIdentifier .get (' unist' ).map (node => node .url ))
85
+ console .log (indexOnIdentifier .get (' unist' ).map (( node ) => node .url ))
84
86
```
85
87
86
88
Yields:
@@ -104,70 +106,97 @@ Yields:
104
106
105
107
## API
106
108
107
- This package exports the identifier ` Index ` .
109
+ This package exports the identifier [ ` Index ` ] [ index ] .
108
110
There is no default export.
109
111
110
112
### ` Index(prop|keyFunction[, tree[, test]]) `
111
113
112
114
Create a mutable index data structure, that maps property values or computed
113
115
keys, to nodes.
114
116
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 ` .
117
119
118
120
###### Parameters
119
121
120
122
* ` prop ` (` string ` )
121
123
— field to look up in each node to find keys
122
- * ` keyFunction ` ([ ` KeyFunction ` ] [ key-function ] )
124
+ * ` keyFunction ` ([ ` KeyFunction ` ] [ keyfunction ] )
123
125
— function called with each node to calculate keys
124
126
* ` tree ` ([ ` Node ` ] [ node ] , optional)
125
127
— tree to index
126
- * ` test ` ([ ` Test ` ] [ is ] , optional)
127
- — [ ` is ` ] [ is ] - compatible test
128
+ * ` test ` ([ ` Test ` ] [ test ] , optional)
129
+ — ` unist-util- is` compatible test
128
130
129
131
###### Returns
130
132
131
133
Instance (` Index ` ).
132
134
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) `
139
136
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 ` .
142
138
143
- #### ` Index#get(key) `
139
+ ###### Parameters
144
140
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 ]
146
144
147
145
###### Returns
148
146
149
147
List of zero or more nodes ([ ` Array<Node> ` ] [ node ] ).
150
148
151
149
#### ` Index#add(node) `
152
150
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
154
157
155
158
###### Returns
156
159
157
160
Current instance (` Index ` ).
158
161
159
162
#### ` Index#remove(node) `
160
163
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
162
170
163
171
###### Returns
164
172
165
173
Current instance (` Index ` ).
166
174
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
+
167
195
## Types
168
196
169
197
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 ] .
171
200
172
201
## Compatibility
173
202
@@ -249,8 +278,12 @@ abide by its terms.
249
278
250
279
[ node ] : https://github.com/syntax-tree/unist#node
251
280
252
- [ is ] : https://github.com/syntax-tree/unist-util-is
253
-
254
281
[ map ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map
255
282
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