hierutil
v3.2.3
Published
hierarchy utility methods
Downloads
1,338
Readme
hierUtil
A TypeScript library providing utilities for hierarchical data manipulation, tree traversal, and data validation. Designed for handling complex tree structures and performing operations such as searching, filtering, and transforming nodes.
Installation
npm install hierutilUsage
Import the desired functions from the library:
import { treeDFSTravers, getAllNodes, searchTreeNodes, checkCondition } from 'hierutil';Key Functions
Tree Traversal and Manipulation
treeDFSTravers(root, func, afterFunc): Performs a depth-first search (DFS) traversal on a tree, applyingfuncto each node and optionallyafterFuncafter processing children.getAllNodes(root, cf='children'): Retrieves all nodes in the tree as an array.getLeafNodes(root, cf='children'): Returns an array of leaf nodes (nodes without children).getSubTreeNodes(root, checkFunc, cf='children'): Collects all nodes in subtrees wherecheckFuncreturns true.getSubTreeRoots(root, checkFunc, cf='children'): Returns the root nodes of subtrees wherecheckFuncreturns true.getTopTree(root, top): Constructs a new tree containing the toptopnodes, preserving parent-child relationships.getPartialTreeNodes(root, list, keyField='RESULT_NODE'): Extracts a partial tree containing nodes fromlistand their ancestors.getExcludeTree({tree, excluded, keyField='RESULT_NODE'}): Returns a tree excluding nodes specified inexcluded.markNodeNum(root, cf='children'): Annotates each node with achildCountproperty representing the number of descendants.cleanTreeNodes(result): Removes specified properties (parent,children,group,groupId) from nodes.
Search Functions
searchTreeNodes(root, field, value, cf='children'): Searches for nodes wherefieldmatchesvalue. Supports single or multiple value searches.findSubTreeNodes(root, checkFunc, cf='children'): Finds the first subtree wherecheckFuncreturns true.treeSearchByHierarchy(root, branches, target, options): Searches for a target node within branches specified bybranches.findFirstLeaf(tree, cf='children'): Returns the first leaf node in a tree.findLastLeaf(tree, cf='children'): Returns the last leaf node in a tree.
Data Validation
checkCondition(sheetData, snapShot, auto, condList): Validates data rows against conditions, supporting multiple versions and dimensions. Returns a status and any exceptions found.compareOper(oper, val1, val2): Compares two values using operators like=,>,<,>=,<=,<>.calcOper(oper, val1, val2): Performs calculations (+,-,*,/,+%,-%) on two values.
Utility Functions
removeBlankBranch(list): Removes hidden nodes and consolidates groups in a list of nodes.removeDupColFields(colFields, axis0List): Removes duplicate column fields based on IDs.printMsg(): Logs a message indicating the library's usage.
Example
// Example tree structure
const tree = {
id: 1,
children: [
{ id: 2, children: [] },
{ id: 3, children: [{ id: 4, children: [] }] }
]
};
// Get all leaf nodes
const leaves = getLeafNodes(tree);
console.log(leaves); // [{ id: 2, children: [] }, { id: 4, children: [] }]
// Search for a node by field value
const found = searchTreeNodes(tree, 'id', 3);
console.log(found); // { id: 3, children: [{ id: 4, children: [] }] }
// Validate data
const sheetData = { rowData: [{ data: 10 }], dateFieldList: ['data'] };
const snapShot = { dimFields: [], onColDims: [{ code: 'data' }], versionCompare: {} };
const auto = { MISC: JSON.stringify({ condJoinType: 'AND' }) };
const condList = [{ condition: 0, compType: 'valueBase', compOper: '>', compInput: 5 }];
const result = checkCondition(sheetData, snapShot, auto, condList);
console.log(result); // { status: false, exceptions: [...] }Notes
- The library assumes a default children field name of
'children', but this can be customized via thecfparameter in most functions. - Functions like
checkConditionare designed for specific data validation scenarios, particularly with multi-version and multi-dimensional data. - Ensure that input data structures are properly formatted to avoid unexpected behavior.
License
Commercial mdxpro.ai
