unist-util-index
Version:
unist utility to index property values or computed keys to nodes
63 lines (62 loc) • 1.67 kB
TypeScript
export class Index {
/**
* Create a mutable index data structure, that maps property values or
* computed keys, to nodes.
*
* If `tree` is given, the index is initialized with all nodes, optionally
* filtered by `test`.
*
* @param {KeyFunction | string} prop
* Field (`string`) to look up in each node to find keys or function called
* with each node to calculate keys.
* @param {Node | null | undefined} [tree]
* Tree to index (optional).
* @param {Test | null | undefined} [test]
* `is`-compatible test (optional).
*/
constructor(
prop: KeyFunction | string,
tree?: Node | null | undefined,
test?: Test | null | undefined
)
/** @type {Map<unknown, Array<Node>>} */
index: Map<unknown, Array<Node>>
/** @type {KeyFunction} */
key: KeyFunction
/**
* Get nodes by `key`.
*
* @param {unknown} key
* Key to retrieve.
*
* Can be anything that can be used as a key in a `Map`.
* @returns {Array<Node>}
* List of zero or more nodes.
*/
get(key: unknown): Array<Node>
/**
* Add `node` to the index (if not already present).
*
* @param {Node} node
* Node to index.
* @returns
* Current instance.
*/
add(node: Node): this
/**
* Remove `node` from the index (if present).
*
* @param {Node} node
* Node to remove.
* @returns
* Current instance.
*/
remove(node: Node): this
}
export type Node = import('unist').Node
export type Test = import('unist-util-visit').Test
/**
* Function called with every added node (`Node`) to calculate the key to
* index on.
*/
export type KeyFunction = (node: Node) => unknown