@loken/hierarchies
Version:
Library for working with hierarchies of identifiers and identifiable objects.
96 lines • 4.74 kB
TypeScript
import { type Some } from '@loken/utilities';
import type { TraversalType } from '../traversal/graph.types.js';
import type { DeBrand, NodePredicate } from './node.types.js';
/**
* Wrapper for an `Item` participating in a double-linked graph.
*
* By using a wrapper rather than require specific properties on the type parameter
* we don't need to make any assumptions about the wrapped type.
*/
export declare class HCNode<Item> {
#private;
/** Create a node wrapping the `item`. */
constructor(item: Item);
/** A node is a "root" when there is no `parent`. */
get isRoot(): boolean;
/** A node is a "leaf" when there are no `children`. */
get isLeaf(): boolean;
/** A node is "internal" when it has `children`, meaning it's either "internal" or a "leaf". */
get isInternal(): boolean;
/**
* A node is "linked" when it is neither a root nor a child.
*
* A node is "linked" when it has a parent or at least one child.
*/
get isLinked(): boolean;
/** Having a brand means that some other entity "owns" the node. */
get isBranded(): boolean;
/**
* When the brands of two nodes are compatible, they may be linked/attached
* in a parent-child relationship.
*/
isBrandCompatible(other: HCNode<Item>): boolean;
/**
* Adds the provided `brand` to the node,
* providing an `DeBrand` delegate for removing/clearing the brand.
*/
brand(brand: any): DeBrand;
/**
* Attach the provided `children`.
*/
attach(children: Some<HCNode<Item>>): this;
/** Detach the provided `children`. */
detach(children: Some<HCNode<Item>>): this;
/** Detach the node from it's `parent`. */
detachSelf(): this;
/**
* Dismantling a node means to cascade detach it.
* We always cascade detach the nodes.
* We may also cascade up the ancestry, in which case the node is detached,
* and then the parent is dismantled, leading to the whole linked structure
* ending up unlinked.
* @param includeAncestry
* Should we cascade through the ancestry (true) or only cascade through the nodes (false)?
* No default value is because the caller should always make an active choice.
*/
dismantle(includeAncestry: boolean): this;
/** The item is the subject/content of the node. */
get item(): Item;
/** Get the parent node, if any. */
getParent(): HCNode<Item> | undefined;
/** Get the parent item, if any. */
getParentItem(): Item | undefined;
/** Get all child nodes. */
getChildren(): HCNode<Item>[];
/** Get all child items. */
getChildItems(): Item[];
/** Get ancestor nodes by traversing according to the options. */
getAncestors(includeSelf?: boolean): HCNode<Item>[];
/** Get ancestor items by traversing according to the options. */
getAncestorItems(includeSelf?: boolean): Item[];
/** Get descendant nodes by traversing according to the options. */
getDescendants(includeSelf?: boolean, type?: TraversalType): HCNode<Item>[];
/** Get descendant items by traversing according to the options. */
getDescendantItems(includeSelf?: boolean, type?: TraversalType): Item[];
/** Find the first ancestor node matching the `search`. */
findAncestor(search: NodePredicate<Item>, includeSelf?: boolean): HCNode<Item> | undefined;
/** Find the ancestor nodes matching the `search`. */
findAncestors(search: NodePredicate<Item>, includeSelf?: boolean): HCNode<Item>[];
/** Find the first descendant node matching the `search`. */
findDescendant(search: NodePredicate<Item>, includeSelf?: boolean, type?: TraversalType): HCNode<Item> | undefined;
/** Find the descendant nodes matching the `search`. */
findDescendants(search: NodePredicate<Item>, includeSelf?: boolean, type?: TraversalType): HCNode<Item>[];
/** Does an ancestor node matching the `search` exist? */
hasAncestor(search: NodePredicate<Item>, includeSelf?: boolean): boolean;
/** Does a descendant node matching the `search` exist? */
hasDescendant(search: NodePredicate<Item>, includeSelf?: boolean, type?: TraversalType): boolean;
/** Generate a sequence of ancestor nodes by traversing according to the options. */
traverseAncestors(includeSelf?: boolean): Generator<HCNode<Item>, void, undefined>;
/** Generate a sequence of descendant nodes by traversing according to the options. */
traverseDescendants(includeSelf?: boolean, type?: TraversalType): Generator<HCNode<Item>, void, unknown>;
/**
* Get nodes to use as roots.
*/
static getRoots<Item>(roots: Some<HCNode<Item>>, includeSelf?: boolean): Some<HCNode<Item>>;
}
//# sourceMappingURL=node.d.ts.map