UNPKG

simple-tree-utils

Version:

Simple Tree Utils is the library to convert and manipulate with tree-like structures.

331 lines (330 loc) 12.7 kB
/** * IConfig interface for configuring whole class during instantiating */ export interface IConfig { /** * Name of unique identifier property in nodes */ idProp?: string; /** * Name of parent identifier property in nodes */ parentIdProp?: string; /** * Name of property where child nodes are stored */ childrenProp?: string; } /** * Class to transform and manipulate tree like structures */ export declare class TreeUtils { /** * Name of unique identifier property in nodes (default value is `id`) */ private readonly idProp; /** * Name of parent identifier property in nodes (default value is `parentId`) */ private readonly parentIdProp; /** * Name of property where child nodes are stored (default value is `children`) */ private readonly childrenProp; /** * Constructor of class * @param config - to configure class, if configuration option is omitted, default one is used */ constructor(config?: IConfig); /** * Convert list to tree like structure * @param list list of objects, objects need to have id (as you configured, or 'id' by default) and parentId property (as you configured, or 'parentId' by default) * @param rootParentId - id of root parent nodes (if not specified, root nodes are nodes with parentId of null) * @returns tree structure */ list2Tree(list: any[], rootParentId?: any): any[]; /** * Convert tree like structure to list * @param tree - tree of objects, objects need to have children (as you configured, or 'children' by default) and parentId property (as you configured, or 'parentId' by default) * @returns list */ tree2List(tree: any[]): any[]; /** * Convert tree like structure to list (helper method) * @param tree tree of objects, objects need to have children (as you configured, or 'children' by default) and parentId property (as you configured, or 'parentId' by default) * @param parentId - id of parent node * @private * @returns list */ private _tree2List; /** * Method to get node in tree structure by given id * @param tree - tree structure to search in * @param id - identifier of node * @returns found node */ get(tree: any[], id: any): any; /** * Method to find node in tree structure by given callback function * @param tree - tree structure to search in * @param fn - callback function to find node * @returns found node * @example * ```ts * utils.find(tree, item => item.id === myId); * ``` */ find(tree: any[], fn: (item: any) => boolean): any; /** * Method to iterate over all nodes * @param tree - tree structure to iterate over * @param fn - callback function to perform */ forEach(tree: any[], fn: (item: any) => any): void; /** * Method to find all nodes in tree structure by given callback function * @param tree - tree structure to search in * @param fn - callback function to find all nodes * @returns all found nodes * @example * ```ts * utils.filter(tree, item => item.id === myId); * ``` */ filter(tree: any[], fn: (item: any) => boolean): any; /** * Method to delete node in tree by given id (mutable operation!) * @param tree - tree structure for node deleting * @param id - identifier of node to delete * @returns deleted node, if nothing deleted then returns null */ delete(tree: any[], id: any): any; /** * Method to delete node in tree by given callback function (mutable operation!) * @param tree - tree structure for node deleting * @param fn - callback function to remove all nodes * @returns deleted nodes * @example * ```ts * utils.deleteBy(tree, item => item.id === myId); * ``` */ deleteBy(tree: any[], fn: (item: any) => boolean): any[]; /** * Method to add new node to tree, node will be added as last child (mutable operation!) * @param tree - tree structure for node adding * @param parentId - identifier of parent node, null if new node should be on root level * @param childData - data of new node * @param anotherChildData - data of new additional nodes */ add(tree: any[], parentId: any, childData: any, ...anotherChildData: any[]): void; /** * Method to add new node to tree, node will be added as first child (mutable operation!) * @param tree - tree structure for node adding * @param parentId - identifier of parent node, null if new node should be on root level * @param childData - data of new node * @param anotherChildData - data of new additional nodes */ addUnshift(tree: any[], parentId: any, childData: any, ...anotherChildData: any[]): void; /** * Method to add new node to tree at the end or at the beginning (helper method) * @param operation - how item should be added into tree * @param tree - tree structure for node adding * @param parentId - identifier of parent node, null if new node should be on root level * @param childData - data of new node * @param anotherChildData - data of new additional nodes */ private _add; /** * Method to update node by id with given data in tree (mutable operation!) * @param tree - tree structure for node editing * @param id - identifier of node to be updated * @param data - new data of node (you should also pass children if you want to keep it) */ edit(tree: any[], id: any, data: any): void; /** * Method to get descendant nodes of given node in tree structure * @param tree - tree structure to search in * @param id - identifier of node * @returns all found children nodes */ getDescendants(tree: any[], id: any): any[]; /** * Helper method to recursively get all descendant nodes of given node in tree structure * @param node - we want to get all of its children * @private * @returns all found children nodes */ private _getDescendants; /** * Method to get ancestors of given node in tree structure * @param tree - tree structure to search in * @param id - identifier of node * @returns all found parent nodes */ getAncestors(tree: any[], id: any): any[]; /** * Method to get nodes that are part of path from root * Alias for {@link TreeUtils.getAncestors | getAncestors} method * @param tree - tree structure to search in * @param id - identifier of node * @returns all nodes that are part of path (ordered from root) */ getPathNodes(tree: any[], id: any): any[]; /** * Method to get parent of given node in tree structure * @param tree - tree structure to search in * @param id - identifier of node * @returns found parent node, otherwise null */ getParent(tree: any[], id: any): any; /** * Method to get parent of given node in tree structure * @param tree - tree structure to search in * @param id - identifier of node * @param parent - parent node, if we found something (for recursion only) * @returns found parent node, otherwise null */ private _getParent; /** * Method to get children of given node in tree structure * @param tree - tree structure to search in * @param id - identifier of node * @returns children nodes of node */ getChildren(tree: any[], id: any): any[]; /** * Method to get neighbours (neighbour is parent or child) of given node in tree structure * @param tree - tree structure to search in * @param id - identifier of node * @returns neighbours of node */ getNeighbours(tree: any[], id: any): any; /** * Method to get siblings of given node in tree structure * @param tree - tree structure to search in * @param id - identifier of node * @returns siblings of node */ getSiblings(tree: any[], id: any): any; /** * Method to get leafs of subtree from given node * @param tree - tree structure to search in * @param id - identifier of node * @returns leafs of nodes */ getLeafs(tree: any[], id: any): any; /** * Method to get subtree from given node (children of node) * Alias for {@link TreeUtils.getChildren | getChildren} method * @param tree - tree structure to search in * @param id - identifier of node * @returns subtree */ getSubTree(tree: any[], id: any): any[]; /** * Method to get size of subtree (number of nodes in the subtree) * @param tree - tree structure * @param id - identifier of node * @returns size of subtree */ getSize(tree: any[], id: any): number; /** * Method to get breath of subtree (the number of leaves in subtree) * @param tree - tree structure * @param id - identifier of node * @returns breath of subtree */ getBreath(tree: any[], id: any): number; /** * Method to get depth of node (the depth of a node is the length of the path to its root i.e., its root path) * @param tree - tree structure * @param id - identifier of node * @returns depth of node */ getDepth(tree: any[], id: any): number; /** * Method to get level of node (the level of a node is the number of edges along the unique path between it and the root node) * @param tree - tree structure * @param id - identifier of node * @returns level of node */ getLevel(tree: any[], id: any): number; /** * Method to get degree of node (for a given node, its number of children. A leaf, by definition, has degree zero) * @param tree - tree structure * @param id - identifier of node * @returns degree of node */ getDegree(tree: any[], id: any): number; /** * Method to get degree of tree (the degree of a tree is the maximum degree of a node in the tree) * @param tree - tree structure * @returns degree of tree */ getTreeDegree(tree: any[]): number; /** * Method to get nodes in tree at specific level * @param tree - tree structure to search in * @param level - desired level * @returns all nodes, that are on specific level */ getNodesAtLevel(tree: any[], level: number): any[]; /** * Helper method to get nodes in tree at specific level recursively * @param tree - tree structure to search in * @param level - desired level * @param actualLevel - actual level, that is searched * @private * @returns all nodes, that are on specific level */ private _getNodesAtLevel; /** * Method to get width on level in tree (the number of nodes in a level) * @param tree - tree structure * @param level - desired level * @returns width on desired level */ getWidth(tree: any[], level: number): number; /** * Method to get height of node (the height of a node is the length of the longest downward path to a leaf from that node) * @param tree - tree structure * @param id - identifier of node * @returns height of node */ getHeight(tree: any[], id: any): number; /** * Helper method to get height of node from children recursively * @param tree - tree structure * @param height - actual computed height * @private * @returns height of node */ private getHeightNode; /** * Method to get distance between 2 nodes * @param tree - tree structure * @param id1 - identifier of first node * @param id2 - identifier of second node * @returns distance between 2 nodes, returns -1, if there is no connection between nodes */ getDistance(tree: any[], id1: any, id2: any): number; /** * Method to compute paths for nodes (mutable operation) * path property will be added into each node * e.g. {path: "parent/child"...} * @param tree - tree structure * @param pathComputationProperty - property to use for path computation * @param delimiter - to delimit path * @param pathProperty - property where path will be stored * @param originPath - path of top level nodes */ computePaths(tree: any[], pathComputationProperty: string, delimiter?: string, pathProperty?: string, originPath?: string): void; /** * Helper method to deep clone object * @param obj - object to be cloned * @private * @returns deep cloned object */ private static deepCopy; }