Skip to content

Commit 607d79d

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent 72c55e5 commit 607d79d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/index.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Node to calculate a key for.
1010
* @returns {unknown}
1111
* Key to index on.
12+
*
1213
* Can be anything that can be used as a key in a `Map`.
1314
*/
1415

@@ -22,20 +23,23 @@ export class Index {
2223
* If `tree` is given, the index is initialized with all nodes, optionally
2324
* filtered by `test`.
2425
*
25-
* @param {string|KeyFunction} prop
26+
* @param {string | KeyFunction} prop
2627
* Field (`string`) to look up in each node to find keys or function called
2728
* with each node to calculate keys.
28-
* @param {Node} [tree]
29+
* @param {Node | null | undefined} [tree]
2930
* Tree to index.
30-
* @param {Test} [test]
31+
* @param {Test | null | undefined} [test]
3132
* `is`-compatible test.
3233
*/
3334
constructor(prop, tree, test) {
3435
/** @type {Map<unknown, Array<Node>>} */
3536
this.index = new Map()
3637
/** @type {KeyFunction} */
37-
// @ts-expect-error: Looks indexable.
38-
this.key = typeof prop === 'string' ? (node) => node[prop] : prop
38+
this.key =
39+
typeof prop === 'string'
40+
? // @ts-expect-error: Looks indexable.
41+
/** @type {KeyFunction} */ ((node) => node[prop])
42+
: prop
3943

4044
if (tree) {
4145
visit(tree, test, (node) => {
@@ -49,6 +53,7 @@ export class Index {
4953
*
5054
* @param {unknown} key
5155
* Key to retrieve.
56+
*
5257
* Can be anything that can be used as a key in a `Map`.
5358
* @returns {Array<Node>}
5459
* List of zero or more nodes.

test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test('Index', (t) => {
2121
'new Index(prop)'
2222
)
2323

24-
// @ts-expect-error: fine.
24+
// @ts-expect-error: Look for nodes with an `id`.
2525
instance = new Index((/** @type {IdNode} */ node) => node.id)
2626
instance.add(node)
2727

@@ -166,7 +166,6 @@ test('index.get', (t) => {
166166
})
167167

168168
t.test('empty index', (st) => {
169-
// @ts-expect-error: runtime.
170169
st.deepEqual(new Index('foo', null).get('bar'), [])
171170
st.deepEqual(new Index('foo').get('bar'), [])
172171
st.end()

0 commit comments

Comments
 (0)