Files
outline/shared/utils/tree.ts
T
Tom Moor bf45e97641 chore: Enforce type import consistency (#10968)
* Update types

* fix circular dep

* type imports

* lint type imports and --fix
2025-12-19 23:07:02 -05:00

37 lines
868 B
TypeScript

import type { NavigationNode } from "../types";
export const flattenTree = (root: NavigationNode) => {
const flattened: NavigationNode[] = [];
if (!root) {
return flattened;
}
flattened.push(root);
root.children.forEach((child) => {
flattened.push(...flattenTree(child));
});
return flattened;
};
export const ancestors = (node: NavigationNode | null) => {
const nodes: NavigationNode[] = [];
if (node) {
while (node.parent !== null) {
nodes.unshift(node.parent as NavigationNode);
node = node.parent as NavigationNode;
}
}
return nodes;
};
export const descendants = (node: NavigationNode, depth = 0) => {
const allDescendants = flattenTree(node).slice(1);
return depth === 0
? allDescendants
: allDescendants.filter(
(d) => (d.depth as number) <= (node.depth as number) + depth
);
};