|
1 | 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`. |
| 2 | + * @typedef {import('./lib/index.js').KeyFunction} KeyFunction |
| 3 | + * @typedef {import('./lib/index.js').Test} Test |
13 | 4 | */
|
14 | 5 |
|
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' |
0 commit comments