Files
outline/app/utils/tree.ts
2023-01-29 10:36:37 -08:00

37 lines
880 B
TypeScript

import { NavigationNode } from "@shared/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 ancestors: NavigationNode[] = [];
if (node) {
while (node.parent !== null) {
ancestors.unshift(node.parent as NavigationNode);
node = node.parent as NavigationNode;
}
}
return ancestors;
};
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
);
};