Skip to content

Commit 72c55e5

Browse files
committed
Refactor to move implementation to lib/
1 parent f2726fa commit 72c55e5

File tree

4 files changed

+110
-105
lines changed

4 files changed

+110
-105
lines changed

index.js

+3-102
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,6 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist-util-visit').Test} Test
4-
*
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`.
2+
* @typedef {import('./lib/index.js').KeyFunction} KeyFunction
3+
* @typedef {import('./lib/index.js').Test} Test
134
*/
145

15-
import {visit} from 'unist-util-visit'
16-
17-
export class Index {
18-
/**
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-
*
25-
* @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.
28-
* @param {Node} [tree]
29-
* Tree to index.
30-
* @param {Test} [test]
31-
* `is`-compatible test.
32-
*/
33-
constructor(prop, tree, test) {
34-
/** @type {Map<unknown, Array<Node>>} */
35-
this.index = new Map()
36-
/** @type {KeyFunction} */
37-
// @ts-expect-error: Looks indexable.
38-
this.key = typeof prop === 'string' ? (node) => node[prop] : prop
39-
40-
if (tree) {
41-
visit(tree, test, (node) => {
42-
this.add(node)
43-
})
44-
}
45-
}
46-
47-
/**
48-
* Get nodes by `key`.
49-
*
50-
* @param {unknown} key
51-
* Key to retrieve.
52-
* Can be anything that can be used as a key in a `Map`.
53-
* @returns {Array<Node>}
54-
* List of zero or more nodes.
55-
*/
56-
get(key) {
57-
return this.index.get(key) || []
58-
}
59-
60-
/**
61-
* Add `node` to the index (if not already present).
62-
*
63-
* @param {Node} node
64-
* Node to index.
65-
* @returns
66-
* Current instance.
67-
*/
68-
add(node) {
69-
const key = this.key(node)
70-
let nodes = this.index.get(key)
71-
72-
if (!nodes) {
73-
nodes = []
74-
this.index.set(key, nodes)
75-
}
76-
77-
if (!nodes.includes(node)) {
78-
nodes.push(node)
79-
}
80-
81-
return this
82-
}
83-
84-
/**
85-
* Remove `node` from the index (if present).
86-
*
87-
* @param {Node} node
88-
* Node to remove.
89-
* @returns
90-
* Current instance.
91-
*/
92-
remove(node) {
93-
const key = this.key(node)
94-
const nodes = this.index.get(key)
95-
96-
if (nodes) {
97-
const pos = nodes.indexOf(node)
98-
if (pos !== -1) {
99-
nodes.splice(pos, 1)
100-
}
101-
}
102-
103-
return this
104-
}
105-
}
6+
export {Index} from './lib/index.js'

lib/index.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist-util-visit').Test} Test
4+
*
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`.
13+
*/
14+
15+
import {visit} from 'unist-util-visit'
16+
17+
export class Index {
18+
/**
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+
*
25+
* @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.
28+
* @param {Node} [tree]
29+
* Tree to index.
30+
* @param {Test} [test]
31+
* `is`-compatible test.
32+
*/
33+
constructor(prop, tree, test) {
34+
/** @type {Map<unknown, Array<Node>>} */
35+
this.index = new Map()
36+
/** @type {KeyFunction} */
37+
// @ts-expect-error: Looks indexable.
38+
this.key = typeof prop === 'string' ? (node) => node[prop] : prop
39+
40+
if (tree) {
41+
visit(tree, test, (node) => {
42+
this.add(node)
43+
})
44+
}
45+
}
46+
47+
/**
48+
* Get nodes by `key`.
49+
*
50+
* @param {unknown} key
51+
* Key to retrieve.
52+
* Can be anything that can be used as a key in a `Map`.
53+
* @returns {Array<Node>}
54+
* List of zero or more nodes.
55+
*/
56+
get(key) {
57+
return this.index.get(key) || []
58+
}
59+
60+
/**
61+
* Add `node` to the index (if not already present).
62+
*
63+
* @param {Node} node
64+
* Node to index.
65+
* @returns
66+
* Current instance.
67+
*/
68+
add(node) {
69+
const key = this.key(node)
70+
let nodes = this.index.get(key)
71+
72+
if (!nodes) {
73+
nodes = []
74+
this.index.set(key, nodes)
75+
}
76+
77+
if (!nodes.includes(node)) {
78+
nodes.push(node)
79+
}
80+
81+
return this
82+
}
83+
84+
/**
85+
* Remove `node` from the index (if present).
86+
*
87+
* @param {Node} node
88+
* Node to remove.
89+
* @returns
90+
* Current instance.
91+
*/
92+
remove(node) {
93+
const key = this.key(node)
94+
const nodes = this.index.get(key)
95+
96+
if (nodes) {
97+
const pos = nodes.indexOf(node)
98+
if (pos !== -1) {
99+
nodes.splice(pos, 1)
100+
}
101+
}
102+
103+
return this
104+
}
105+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"main": "index.js",
3838
"types": "index.d.ts",
3939
"files": [
40+
"lib/",
4041
"index.d.ts",
4142
"index.js"
4243
],

test.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/**
2-
* @typedef {import('./index.js').Node} Node
3-
* @typedef {import('./index.js').KeyFunction} KeyFunction
4-
* @typedef {import('./index.js').Test} Test
2+
* @typedef {import('unist').Node} Node
53
* @typedef {Node & {id: number}} IdNode
64
*/
75

0 commit comments

Comments
 (0)