Functions for navigating the hierarchical structure of graphs with parent-child relationships.
getChildren
Returns direct children of a node in the hierarchy. Pass null to get root-level nodes.
function getChildren<N>(
graph: Graph<N>,
nodeId: string | null
): GraphNode<N>[]
ID of the parent node, or null for root-level nodes
import { createGraph, getChildren } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'parent' },
{ id: 'child1', parentId: 'parent' },
{ id: 'child2', parentId: 'parent' },
],
});
const children = getChildren(graph, 'parent');
// => [node child1, node child2]
const roots = getChildren(graph, null);
// => [node parent]
getParent
Returns the parent node in the hierarchy, or undefined if the node is at root level.
function getParent<N>(
graph: Graph<N>,
nodeId: string
): GraphNode<N> | undefined
ID of the node to get parent for
The parent node, or undefined if none
import { createGraph, getParent } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'parent' },
{ id: 'child', parentId: 'parent' },
],
});
const parent = getParent(graph, 'child');
// => node parent
const noParent = getParent(graph, 'parent');
// => undefined
getAncestors
Returns all ancestors from the node up to the root (nearest parent first).
function getAncestors<N>(
graph: Graph<N>,
nodeId: string
): GraphNode<N>[]
ID of the node to get ancestors for
Array of ancestor nodes, ordered from nearest to furthest (root last)
import { createGraph, getAncestors } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'root' },
{ id: 'mid', parentId: 'root' },
{ id: 'leaf', parentId: 'mid' },
],
});
const ancestors = getAncestors(graph, 'leaf');
// => [node mid, node root]
getDescendants
Returns all descendants recursively (depth-first).
function getDescendants<N>(
graph: Graph<N>,
nodeId: string
): GraphNode<N>[]
ID of the node to get descendants for
Array of all descendant nodes (children, grandchildren, etc.)
import { createGraph, getDescendants } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'root' },
{ id: 'child', parentId: 'root' },
{ id: 'grandchild', parentId: 'child' },
],
});
const descendants = getDescendants(graph, 'root');
// => [node child, node grandchild]
getSiblings
Returns sibling nodes (same parentId, excluding the node itself).
function getSiblings<N>(
graph: Graph<N>,
nodeId: string
): GraphNode<N>[]
ID of the node to get siblings for
import { createGraph, getSiblings } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'parent' },
{ id: 'a', parentId: 'parent' },
{ id: 'b', parentId: 'parent' },
{ id: 'c', parentId: 'parent' },
],
});
const siblings = getSiblings(graph, 'a');
// => [node b, node c]
getRoots
Returns all root nodes (nodes with no parent).
function getRoots<N>(graph: Graph<N>): GraphNode<N>[]
Array of all root-level nodes
import { createGraph, getRoots } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'root1' },
{ id: 'root2' },
{ id: 'child', parentId: 'root1' },
],
});
const roots = getRoots(graph);
// => [node root1, node root2]
getDepth
Returns the depth of a node in the hierarchy (root = 0). Returns -1 if the node is not found.
function getDepth(graph: Graph, nodeId: string): number
ID of the node to get depth for
Depth level (0 for root, -1 if not found)
import { createGraph, getDepth } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'root' },
{ id: 'child', parentId: 'root' },
{ id: 'grandchild', parentId: 'child' },
],
});
getDepth(graph, 'root'); // => 0
getDepth(graph, 'child'); // => 1
getDepth(graph, 'grandchild'); // => 2
getLCA
Finds the Least Common Ancestor — the deepest proper ancestor shared by all given nodes. A proper ancestor excludes the input nodes themselves.
function getLCA<N>(
graph: Graph<N>,
...nodeIds: string[]
): GraphNode<N> | undefined
IDs of nodes to find common ancestor for
The lowest common ancestor node, or undefined if none
import { createGraph, getLCA } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'root' },
{ id: 'a', parentId: 'root' },
{ id: 'b', parentId: 'root' },
{ id: 'a1', parentId: 'a' },
],
});
const lca1 = getLCA(graph, 'a1', 'b');
// => node root
const lca2 = getLCA(graph, 'a', 'b');
// => node root
isCompound
Returns whether a node has children (is a compound/group node).
function isCompound(graph: Graph, nodeId: string): boolean
true if the node has children, false otherwise
import { createGraph, isCompound } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'parent' },
{ id: 'child', parentId: 'parent' },
],
});
isCompound(graph, 'parent'); // => true
isCompound(graph, 'child'); // => false
isLeaf
Returns whether a node has no children (is a leaf/atomic node).
function isLeaf(graph: Graph, nodeId: string): boolean
true if the node has no children, false otherwise
import { createGraph, isLeaf } from '@statelyai/graph';
const graph = createGraph({
nodes: [
{ id: 'parent' },
{ id: 'child', parentId: 'parent' },
],
});
isLeaf(graph, 'child'); // => true
isLeaf(graph, 'parent'); // => false