-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
54 lines (45 loc) · 1.43 KB
/
data.js
File metadata and controls
54 lines (45 loc) · 1.43 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { generateTree } from './src/lib/generate-tree.js';
fetch('https://api.github.com/repos/sveltejs/svelte/git/trees/main?recursive=1').then(response => response.json()).then(data => {
const result = data.tree.map((content, index) => {
const depth = content.path.split('/');
const textIndex = depth.length;
return {
id: index + 1,
depth: textIndex,
text: depth[textIndex - 1],
depthIndicator: '',
}
});
const treeStructure = convertToTree(result);
console.log(treeStructure);
console.log(generateTree(treeStructure));
});
function convertToTree(items) {
// Create a root node
const root = {
name: '.',
children: [],
indentCount: -1,
parent: null
};
let stack = [root];
items.forEach(item => {
const node = {
name: item.text,
children: [],
indentCount: item.depth - 1, // Convert depth into indentCount
parent: null
}
// Find the right parent
while (stack.length > 1 && stack[stack.length - 1].indentCount >= node.indentCount) {
stack.pop();
}
// Set the parent-child relationship
const parent = stack[stack.length - 1];
parent.children.push(node);
node.parent = parent;
// Add to stack if it could be a parent
stack.push(node);
});
return root;
}