-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileTree.ts
25 lines (24 loc) · 973 Bytes
/
fileTree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
export interface TreeNode {
label: string;
nodes: TreeNode[];
}
export const generateAsciiTree = (treeData: TreeNode): string => {
//This is the function that generates the tree structure from the tree data
const lines: string[] = [];
const traverse = (node: TreeNode, prefix = "", nextPrefix = "") => {
lines.push(prefix + node.label);
const isLastChild = (index: number) => index === node.nodes.length - 1;
node.nodes.forEach((childNode, index) => {
const childPrefix = isLastChild(index) ? "└─ " : "├─ "; //This is the prefix that is used to draw the tree structure
const nextChildPrefix = isLastChild(index) ? " " : "│ "; //This is the prefix that is used to draw the tree structure
traverse(
//This is the recursive call to traverse the tree
childNode,
nextPrefix + childPrefix,
nextPrefix + nextChildPrefix
);
});
};
traverse(treeData);
return lines.join("\n");
};