1.2.0 • Published 2 years ago

@orioro/tree-model v1.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

treeModel

npm install @orioro/tree-model
yarn add @orioro/tree-model

API Docs

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)
tree.isDescendantOf(nodesById, candidateDescendantId, candidateAncestorId)
tree.childIds(nodesById, nodeId)
tree.isChildOf(nodesById, nodeId, candidateParentNodeId)
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)
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

1.2.0

2 years ago

1.1.1

3 years ago

1.1.0

4 years ago

1.0.0

4 years ago