-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertString.js
More file actions
77 lines (74 loc) · 2.38 KB
/
convertString.js
File metadata and controls
77 lines (74 loc) · 2.38 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class node {
constructor(path, type, name, toggled, content) {
this.path = path;
this.type = type;
this.name = name;
this.toggled = toggled;
this.content = content;
this.children = [];
this.modified = false;
}
addChildren(childrenNode) {
this.children.push(childrenNode);
}
}
class leafNode {
constructor(node) {
this.path = node.path;
this.type = node.type;
this.name = node.name;
this.toggled = node.toggled;
this.content = node.content;
this.oldcontent = node.content;
this.modified = node.modified;
}
}
addNode = (node1, node2) => {
if (node1.type !== 'folder') return;
let isExist = false;
for (let i = 0; i < node1.children.length; i ++) {
let node3 = node1.children[i];
if (node2.path.includes(node3.path)) {
isExist = true;
addNode(node3, node2);
}
}
if (!isExist) node1.addChildren(node2);
}
convertString = (input) => {
let nameNode = input.url.split('/')[5];
let rootNode = new node('/', 'folder', nameNode, true, '');
let nodes = [];
for (let i = 0; i < input.tree.length; i ++) {
input.tree[i].path = '/' + input.tree[i].path;
let path = input.tree[i].path.split('/');
let name = path[path.length - 1];
let type;
if (input.tree[i].type === 'tree') type = 'folder';
else {
if (!name.includes('.')) type = 'file';
else {
let nameEx = name.split('.');
type = nameEx[nameEx.length - 1];
if (type === 'js') type = 'javascript';
}
}
nodes[i] = new node(input.tree[i].path, type, name, false, (type !== 'folder')? input.tree[i].url : '');
if (nodes[i].type !== 'folder') nodes[i] = new leafNode(nodes[i]);
else nodes[i].path = nodes[i].path + '/';
}
for (let i = 0; i < input.tree.length; i ++) {
if (nodes[i].name === 'README.md') {
nodes.push(nodes[i]);
nodes.splice(i, 1);
}
}
for (let i = 0; i < input.tree.length; i ++) {
if (nodes[i].type === 'folder') addNode(rootNode, nodes[i]);
}
for (let i = 0; i < input.tree.length; i ++) {
if (nodes[i].type !== 'folder') addNode(rootNode, nodes[i]);
}
return rootNode;
}
module.exports = convertString;