@orioro/tree-model v0.0.2
treeModel
npm install @orioro/tree-model
yarn add @orioro/tree-modelAPI Docs
src/types.ts
src/structure.ts
src/traversal.ts
tree.isRoot(nodesById, nodeId)tree.rootNodeId(nodesById)tree.ancestorIds(nodesById, nodeId)tree.nodePath(nodesById, nodeId)tree.isAncestorOf(nodesById, candidateAncestorId, candidateDescendantId)tree.isDescendantOf(nodesById, candidateDescendantId, candidateAncestorId)tree.childIds(nodesById, nodeId)tree.isChildOf(nodesById, nodeId, candidateChildNodeId)tree.siblingIds(nodesById, nodeId)tree.isSiblingOf(nodesById, nodeId, otherNodeId)tree.commonPath(nodesById, nodeIds)tree.commonAncestorPath(nodesById, nodeIds)tree.commonAncestorId(nodesById, nodeIds)
src/util.ts
stateCache()
Creates a cache getter that memoizes the last state and returns corresponding cache object. If the state object changes, the previous cache object is abandoned and a new cache object is returned.
The cache object itself is a JS Map
- Returns: 
getStateCache{Function} 
memoizeStateQuery(queryFn, keyResolver)
Memoizes a function that takes as first argument the state object.
If the state changes (by identity/reference comparison), cache is reset.
Calls to the memoized function attempt to locate a result in the cache object.
queryFn{Function}keyResolver{Function}
tree.nodeIdArray(nodesById)
nodesById{TreeState}- Returns: {[Node](#node)Id[]}
 
tree.nodeArray(nodesById)
tree.nodeTree(nodesById, nodeId)
nodesById{TreeState}nodeId{[Node](#node)Id}- Returns: {[Node](#node)Tree}
 
tree.isRoot(nodesById, nodeId)
Returns whether the given node is the root node (parentId === null)
nodesById{TreeState}nodeId{[Node](#node)Id}- Returns: {Boolean}
 
tree.rootNodeId(nodesById)
nodesById{TreeState}- Returns: {[Node](#node)Id}
 
tree.ancestorIds(nodesById, nodeId)
Retrieves the list of node ancestors by walking up the tree
using node.parentId
nodesById{TreeState}nodeId{[Node](#node)Id}- Returns: {[Node](#node)Id[]}
 
tree.nodePath(nodesById, nodeId)
Returns the full path up to the node. A node path is an array of nodeIds in sequence.
nodesById{TreeState}nodeId{[Node](#node)Id}- Returns: {[Node](#node)Path}
 
tree.isAncestorOf(nodesById, candidateAncestorId, candidateDescendantId)
nodesById{TreeState}candidateAncestorId{[Node](#node)Id}candidateDescendantId{[Node](#node)Id}- Returns: {Boolean}
 
tree.isDescendantOf(nodesById, candidateDescendantId, candidateAncestorId)
nodesById{TreeState}candidateDescendantId{[Node](#node)Id}candidateAncestorId{[Node](#node)Id}- Returns: {Boolean}
 
tree.childIds(nodesById, nodeId)
nodesById{TreeState}nodeId{[Node](#node)Id}- Returns: {[Node](#node)Id[]}
 
tree.isChildOf(nodesById, nodeId, candidateParentNodeId)
nodesById{TreeState}nodeId{[Node](#node)Id}candidateParentNodeId{[Node](#node)Id}- Returns: {Boolean}
 
tree.isChildOf(nodesById, nodeId, candidateChildNodeId)
nodesById{TreeState}nodeId{[Node](#node)Id}candidateChildNodeId{[Node](#node)Id}- Returns: {Boolean}
 
tree.siblingIds(nodesById, nodeId)
nodesById{TreeState}nodeId{[Node](#node)Id}- Returns: {[Node](#node)Id[]}
 
tree.isSiblingOf(nodesById, nodeId, otherNodeId)
nodesById{TreeState}nodeId{[Node](#node)Id}otherNodeId{[Node](#node)Id}- Returns: {Boolean}
 
tree.commonPath(nodesById, nodeIds)
nodesById{TreeState}nodeIds{[Node](#node)Id[]}- Returns: {[Node](#node)Path}
 
tree.commonAncestorPath(nodesById, nodeIds)
nodesById{TreeState}nodeIds{[Node](#node)Id[]}- Returns: {[Node](#node)Path}
 
tree.commonAncestorId(nodesById, nodeIds)
nodesById{TreeState}nodeIds{[Node](#node)Id[]}- Returns: {[Node](#node)Id | null}
 
NodeId
String
NodePath
A sequence of nodeIds that lead to the last node
Node
{
  id: NodeId
  parentId: NodeId | null
  [key: string]: any
}TreeState
Index of all nodes that compose the tree indexed by id:
{ [key: string]: [Node](#node) }
NodeTree
Tree representation of the node
[Node, NodeTree[] | null]
fromNodeArray(nodeArray)
Generates a tree state (nodes indexed by id) from an array of node objects.
commonPath(paths)
Given a set of NodePaths, return the longest common path
paths{[Node](#node)Path[]}- Returns: 
commonPath{[Node](#node)Path}