UNPKG

@loken/hierarchies

Version:

Library for working with hierarchies of identifiers and identifiable objects.

148 lines 8.3 kB
import { MultiMap, type Some } from '@loken/utilities'; import type { TraversalType } from '../traversal/graph.types.js'; import type { Identify } from '../utilities/identify.js'; import type { GetChildren, GetParent } from '../utilities/related-items.js'; import type { Relation } from '../utilities/relations.js'; import { HCNode } from './node.js'; import type { NodePredicate } from './node.types.js'; export declare class Nodes { /** * Create one or more nodes. * * @items One or more `Item`s to wrap in nodes. * @returns One node when you pass one item and a fixed length tuple of nodes when you pass multiple items. * @throws Must provide at least one argument. */ static create<Items extends any[]>(...items: Items): import("@loken/utilities").MapArgs<Items, HCNode<import("@loken/utilities").ItemOfList<Items, unknown>>, true, false>; /** * Create some nodes. * * @items One or more `Item`s to wrap in nodes. * @returns An array of nodes containing the items. * @throws Must provide at least one argument. */ static createSome<Item>(items: Some<Item>): HCNode<Item>[]; /** * Build nodes of IDs linked as described by the provided `childMap`. * * @template Id The type of IDs. * @param childMap The map describing the relations. * @returns The root nodes. */ static assembleIds<Id>(childMap: MultiMap<Id>): HCNode<Id>[]; /** * Build nodes of IDs recursively from the property keys. * * @param source The object describing the relations. * @param include Optional predicate used for determining whether a property should be included as an ID. * @returns The root nodes. */ static assemblePropertyIds(source: object, include?: (prop: string, val: any) => boolean): HCNode<string>[]; /** * Build nodes of `items` linked as described by the provided `childMap`. * * Will exclude any `items` that are not mentioned in the `childMap`. * * @template Item The type of item. * @template Id The type of IDs. * @param items The items to wrap in nodes. * @param identify Means of getting an ID for an item. * @param childMap The map describing the relations. * @returns The root nodes. */ static assembleItems<Item, Id>(items: Some<Item>, identify: Identify<Item, Id>, childMap: MultiMap<Id>): HCNode<Item>[]; /** * Build nodes of `Item`s linked as described by the provided `roots` and `children` delegate. * * @template Item The type of item. * @template Id The type of IDs. * @param roots The root items to wrap in nodes. * @param children The delegate for getting the child items from a parent item. * @returns The root nodes. */ static assembleItemsWithChildren<Item>(roots: Some<Item>, children: GetChildren<Item>): HCNode<Item>[]; /** * Build nodes of ´Item´s linked as described by the provided `leaves` and `parent` delegate. * * @template Item The type of item. * @template Id The type of IDs. * @param leaves The leaf items to wrap in nodes. * @param parent The delegate for getting the parent of an item. * @returns The root nodes. */ static assembleItemsWithParents<Item>(leaves: Some<Item>, parent: GetParent<Item>): HCNode<Item>[]; /** * Create a map of ids to child-ids by traversing the graph of the `roots`. * * @template Item The type of item. * @template Id The type of IDs. * @param roots The roots to use for traversal. * @param identify Means of getting an ID for an item. * @param childMap An existing or new child-map. * @returns A parent-to-child map of IDs. */ static toChildMap<Item, Id = Item>(roots: Some<HCNode<Item>>, identify?: Identify<Item, Id>, childMap?: MultiMap<Id>): MultiMap<Id>; /** * Create a map of ids to descendant-ids by traversing the graph of the `roots`. * * @template Item The type of item. * @template Id The type of IDs. * @param roots The roots to use for traversal. * @param identify Means of getting an ID for an item. * @param descendantMap An existing or new descendant-map. * @returns A parent-to-descendant map of IDs. */ static toDescendantMap<Item, Id = Item>(roots: Some<HCNode<Item>>, identify?: Identify<Item, Id>, descendantMap?: MultiMap<Id>): MultiMap<Id>; /** * Create a map of ids to ancestor-ids by traversing the graph of the `roots`. * * @template Item The type of item. * @template Id The type of IDs. * @param roots The roots to use for traversal. * @param identify Means of getting an ID for an item. * @param ancestorMap An existing or new ancestor-map. * @returns A child-to-ancestor map of IDs. */ static toAncestorMap<Item, Id = Item>(roots: Some<HCNode<Item>>, identify?: Identify<Item, Id>, ancestorMap?: MultiMap<Id>): MultiMap<Id>; /** * Create a list of relations by traversing the graph of the `roots`. * * @template Item The type of item. * @template Id The type of IDs. * @param roots The roots to use for traversal. * @param identify Means of getting an ID for an item. * @returns An array of `Relation<Id>`s. */ static toRelations<Item, Id = Item>(roots: Some<HCNode<Item>>, identify?: Identify<Item, Id>): Relation<Id>[]; /** Get unique ancestor nodes. */ static getAncestors<Item>(nodes: Some<HCNode<Item>>, includeSelf?: boolean): HCNode<Item>[]; /** Get ancestor items from unique ancestor nodes. */ static getAncestorItems<Item>(nodes: Some<HCNode<Item>>, includeSelf?: boolean): Item[]; /** Get descendant nodes by traversing according to the options. */ static getDescendants<Item>(roots: Some<HCNode<Item>>, includeSelf?: boolean, type?: TraversalType): HCNode<Item>[]; /** Get descendant items by traversing according to the options. */ static getDescendantItems<Item>(roots: Some<HCNode<Item>>, includeSelf?: boolean, type?: TraversalType): Item[]; /** Generate a sequence of descendant nodes by traversing according to the options. */ static traverseDescendants<Item>(roots: Some<HCNode<Item>>, includeSelf?: boolean, type?: TraversalType): Generator<HCNode<Item>, void, undefined>; /** Find the common ancestor node which is the closest to the `nodes`. */ static findCommonAncestor<Item>(nodes: Some<HCNode<Item>>, includeSelf?: boolean): HCNode<Item> | undefined; /** Find the ancestor nodes common to the `nodes`. */ static findCommonAncestors<Item>(nodes: Some<HCNode<Item>>, includeSelf?: boolean): HCNode<Item>[] | undefined; /** Find the ancestor nodes common to the `nodes`. */ static findCommonAncestorItems<Item>(nodes: Some<HCNode<Item>>, includeSelf?: boolean): Item[] | undefined; /** Find the set of ancestor nodes common to the `nodes`. */ static findCommonAncestorSet<Item>(nodes: Some<HCNode<Item>>, includeSelf?: boolean): Set<HCNode<Item>> | null; /** Find the first ancestor node matching the `search`. */ static findAncestor<Item>(roots: Some<HCNode<Item>>, search: NodePredicate<Item>, includeSelf?: boolean): HCNode<Item> | undefined; /** Find the first ancestor node matching the `search`. */ static findAncestors<Item>(roots: Some<HCNode<Item>>, search: NodePredicate<Item>, includeSelf?: boolean): HCNode<Item>[]; /** Find the first descendant node matching the `search`. */ static findDescendant<Item>(roots: Some<HCNode<Item>>, search: NodePredicate<Item>, includeSelf?: boolean, type?: TraversalType): HCNode<Item> | undefined; /** Find the descendant nodes matching the `search`. */ static findDescendants<Item>(roots: Some<HCNode<Item>>, search: NodePredicate<Item>, includeSelf?: boolean, type?: TraversalType): HCNode<Item>[]; /** Does an ancestor node matching the `search` exist? */ static hasAncestor<Item>(roots: Some<HCNode<Item>>, search: NodePredicate<Item>, includeSelf?: boolean): boolean; /** Does a descendant node matching the `search` exist? */ static hasDescendant<Item>(roots: Some<HCNode<Item>>, search: NodePredicate<Item>, includeSelf?: boolean, type?: TraversalType): boolean; } //# sourceMappingURL=nodes.d.ts.map