UNPKG

react-select-table

Version:
128 lines (127 loc) 5.04 kB
/** * @namespace DlMapTypes */ /** * @template Key, Value, Metadata * @typedef {object} DlMapTypes.Node * @property {Value} value The value of the item * @property {Metadata} metadata Metadata for the item * @property {Key} prevKey The key of the previous item * @property {Key} nextKey The key of the next item */ /** * A kind of {@link https://www.geeksforgeeks.org/linkedhashmap-class-in-java/|LinkedHashMap} * * @template Key, Value, Metadata * @typedef {object} DlMapTypes.Map * @property {Key} headKey The key of the first item * @property {Key} tailKey The key of the last item * @property {Object<Key, DlMapTypes.Node<Key, Value, Metadata>>} nodes The map nodes */ /** * Returns a new doubly linked map instance * * @template Key, Value, Metadata * @returns {DlMapTypes.Map<Key, Value, Metadata>} A new instance */ export function instance<Key, Value, Metadata>(): DlMapTypes.Map<Key, Value, Metadata>; /** * Returns the next or previous key of the one given, and the first or last key if a key isn't given * * @template Key, Value, Metadata * @generator * @param {DlMapTypes.Map<Key, Value, Metadata>} map A map instance * @param {boolean} [forward=true] True to yield the next key, or false to yield the previous one * @param {Key} [key] The key to start from, or null to start from the beginning * @yields {Key?} The next or previous key */ export function keyIterator<Key, Value, Metadata>(map: DlMapTypes.Map<Key, Value, Metadata>, forward?: boolean | undefined, key?: Key | undefined): Generator<Key, void, unknown>; /** * @template Key * @callback KeyComparator * @param {Key} first The key of an item * @param {Key} second The key of another item * @returns {number} * Should return: * - 1, if the first item should be placed after the second * - -1, if the first item should be placed before the second */ /** * Links items inserted using {@link addUnlinkedItem} in the correct position * * @template Key, Value, Metadata * @param {DlMapTypes.Map<Key, Value, Metadata>} map A map instance * @param {Key[]} keys The keys of the items to link * @param {KeyComparator} keyComparator A function that compares two items */ export function sortAndLinkItems<Key, Value, Metadata>(map: DlMapTypes.Map<Key, Value, Metadata>, keys: Key[], keyComparator: any): void; /** * Deletes an item from the map by its key * * @template Key, Value, Metadata * @param {DlMapTypes.Map<Key, Value, Metadata>} map A map instance * @param {Key} key The key of the item to delete * @returns {Value|void} Returns the value of the item if it existed, or undefined if it didn't */ export function deleteItem<Key, Value, Metadata>(map: DlMapTypes.Map<Key, Value, Metadata>, key: Key): void | Value; /** * Adds an item to the map, but doesn't link it yet. The linking is done by {@link sortAndLinkItems} * * @template Key, Value, Metadata * @param {DlMapTypes.Map<Key, Value, Metadata>} map A map instance * @param {Key} key The new of the new item * @param {Value} value The value of the new item * @param {Metadata} metadata The metadata of the new item * @returns {boolean} Returns true if an item with the same value existed and was replaced */ export function addUnlinkedItem<Key, Value, Metadata>(map: DlMapTypes.Map<Key, Value, Metadata>, key: Key, value: Value, metadata: Metadata): boolean; /** * Changes the order of the items according to the new key comparator * * @template Key, Value, Metadata * @param {DlMapTypes.Map<Key, Value, Metadata>} map A map instance * @param {KeyComparator} keyComparator A function that compares two items */ export function sortItems<Key, Value, Metadata>(map: DlMapTypes.Map<Key, Value, Metadata>, keyComparator: any): void; export function getItems<Key, Value, Metadata>(map: DlMapTypes.Map<Key, Value, Metadata>): Value[]; export function getKeyedItems<Key, Value, Metadata>(map: DlMapTypes.Map<Key, Value, Metadata>): any; export function getItem<Key, Value, Metadata>(map: DlMapTypes.Map<Key, Value, Metadata>, key: Key): Value | null; export function getItemMetadata<Key, Value, Metadata>(map: DlMapTypes.Map<Key, Value, Metadata>, key: Key): Metadata | null; export namespace DlMapTypes { type Node<Key, Value, Metadata> = { /** * The value of the item */ value: Value; /** * Metadata for the item */ metadata: Metadata; /** * The key of the previous item */ prevKey: Key; /** * The key of the next item */ nextKey: Key; }; /** * A kind of {@link https://www.geeksforgeeks.org/linkedhashmap-class-in-java/|LinkedHashMap} */ type Map<Key, Value, Metadata> = { /** * The key of the first item */ headKey: Key; /** * The key of the last item */ tailKey: Key; /** * The map nodes */ nodes: any; }; } export type KeyComparator<Key> = (first: Key, second: Key) => number;