@orioro/tree-model
v1.2.0
Published
``` npm install @orioro/tree-model yarn add @orioro/tree-model ```
Downloads
4
Readme
treeModel
npm install @orioro/tree-model
yarn add @orioro/tree-model
API 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)
tree.nodeArray(nodesById)
tree.nodeTree(nodesById, nodeId)
tree.isRoot(nodesById, nodeId)
Returns whether the given node is the root node (parentId === null)
tree.rootNodeId(nodesById)
tree.ancestorIds(nodesById, nodeId)
Retrieves the list of node ancestors by walking up the tree
using node.parentId
tree.nodePath(nodesById, nodeId)
Returns the full path up to the node. A node path is an array of nodeIds in sequence.
tree.isAncestorOf(nodesById, candidateAncestorId, candidateDescendantId)
nodesById
{TreeState}candidateAncestorId
{[NodeId](#nodeid)}candidateDescendantId
{[NodeId](#nodeid)}- Returns: {Boolean}
tree.isDescendantOf(nodesById, candidateDescendantId, candidateAncestorId)
nodesById
{TreeState}candidateDescendantId
{[NodeId](#nodeid)}candidateAncestorId
{[NodeId](#nodeid)}- Returns: {Boolean}
tree.childIds(nodesById, nodeId)
tree.isChildOf(nodesById, nodeId, candidateParentNodeId)
nodesById
{TreeState}nodeId
{[NodeId](#nodeid)}candidateParentNodeId
{[NodeId](#nodeid)}- Returns: {Boolean}
tree.isChildOf(nodesById, nodeId, candidateChildNodeId)
nodesById
{TreeState}nodeId
{[NodeId](#nodeid)}candidateChildNodeId
{[NodeId](#nodeid)}- Returns: {Boolean}
tree.siblingIds(nodesById, nodeId)
tree.isSiblingOf(nodesById, nodeId, otherNodeId)
tree.commonPath(nodesById, nodeIds)
tree.commonAncestorPath(nodesById, nodeIds)
tree.commonAncestorId(nodesById, nodeIds)
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