UNPKG

@loken/hierarchies

Version:

Library for working with hierarchies of identifiers and identifiable objects.

72 lines 4.2 kB
import { MultiMap, type Some } from '@loken/utilities'; import { Hierarchy } from '../hierarchies/hierarchy.js'; import type { Identify } from './identify.js'; import type { IdSpec, ItemIdOptions } from './identity-options.js'; import type { GetChildren, GetParent, IdentifyChildren, IdentifyParent } from './related-items.js'; import type { Relation } from './relations.js'; /** * Static helper for mapping a `MultiMap<Id>` representing a parent-to-child map, * and other representations of the same information. */ export declare class ChildMap { /** Block instantiation by making the ctor private to simulate a static class. */ private constructor(); /** Create a child-map from an `IdSpec`. */ static fromIds<Id>(spec: IdSpec<Id>): MultiMap<Id>; /** * Create a child-map from the nested property keys of the `source`. * * @param source The object describing the relations. * @param include Optional predicate used for determining whether a property should be included as an ID. */ static fromPropertyIds(source: object, include?: (prop: string, val: any) => boolean): MultiMap<string>; /** Create a child-map from `ItemIdOptions`. */ static fromItems<Item, Id>(options: ItemIdOptions<Item, Id>): MultiMap<Id>; /** Create a child-map from `items` using `identify` and `getChildren` delegates. */ static fromChildren<Item, Id>(items: Some<Item>, identify: Identify<Item, Id>, getChildren: GetChildren<Item>): MultiMap<Id>; /** Create a child-map from `items` using `identify` and `identifyChildren` delegates. */ static fromChildIds<Item, Id>(items: Some<Item>, identify: Identify<Item, Id>, identifyChildren: IdentifyChildren<Item, Id>): MultiMap<Id>; /** Create a child-map from `items` using `identify` and `getParent` delegates. */ static fromParents<Item, Id>(items: Some<Item>, identify: Identify<Item, Id>, getParent: GetParent<Item>): MultiMap<Id>; /** Create a child-map from `items` using `identify` and `identifyParent` delegates. */ static fromParentIds<Item, Id>(items: Some<Item>, identify: Identify<Item, Id>, identifyParent: IdentifyParent<Item, Id>): MultiMap<Id>; /** Create a child map from the `hierarchy`. */ static fromHierarchy<Item, Id>(hierarchy: Hierarchy<Item, Id>): MultiMap<Id>; /** Create a child map from the `relations`. */ static fromRelations<Id>(relations: Some<Relation<Id>>): MultiMap<Id>; /** Create relations from the `childMap`. */ static toRelations<Id>(childMap: MultiMap<Id>): Relation<Id>[]; /** Create a parent map from the `childMap`. */ static toParentMap<Id>(childMap: MultiMap<Id>, roots?: Set<Id>): Map<Id, Id | undefined>; /** Create a descendants map from the `childMap`. */ static toDescendantMap<Id>(childMap: MultiMap<Id>, parentMap?: Map<Id, Id | undefined>): MultiMap<Id>; /** Create an ancestor map from the `childMap`. */ static toAncestorMap<Id>(childMap: MultiMap<Id>, parentMap?: Map<Id, Id | undefined>): MultiMap<Id>; /** Get the set of IDs representing the roots of the `childMap`. */ static getRoots<Id>(childMap: MultiMap<Id>): Set<Id>; /** * Add the `ancestors` to the `childMap`. * @param ancestors The ancestor IDs organized from a node and up through its parents. * @param childMap A child-map. Default: Empty `MultiMap`. * @returns The `childMap`. */ static addAncestors<Id>(ancestors: Id[], childMap?: MultiMap<Id>): MultiMap<Id> | undefined; /** * Generate a child map for `count` IDs using a `create` function and randomly created structure. * @param count The number of IDs to create. * @param create The delegate responsible for creating new IDs. * @param chance Fraction chance to add move a layer deeper in the tree. (Default: 0.50) * @param decay The decay to apply to the `chance` for each layer. */ static generate<Id>(options: { count: number; create: (options: ({ index: number; siblings: Id[]; ancestry: Id[]; })) => Id; chance?: number; decay?: number; }): MultiMap<Id>; } //# sourceMappingURL=child-map.d.ts.map