-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectoryOutput.js
156 lines (152 loc) · 3.91 KB
/
directoryOutput.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Given a JSON object, where each entry represents a directory which have a nested entry of it's own. Create the resulting directory structure */
const directoryRoot = [
{
type: "folder",
name: "root",
path: "/root",
children: [
{
type: "folder",
name: "Downloads",
path: "/downloads",
children: [
{
type: "file",
name: "movie.mp4",
path: "/movie",
children: []
}
]
},
{
type: "folder",
name: "Documents",
path: "/documents",
children: [
{
type: "folder",
name: "app",
path: "/app",
children: [
{
type: "file",
name: "index.html",
path: "/index.html",
children: []
},
{
type: "folder",
name: "src",
path: "/src",
children: [
{
type: "file",
name: "index.js",
path: "/index.js",
children: []
}
]
}
]
}
]
},
{
type: "folder",
name: "Pictures",
path: "/pictures",
children: [
{
type: "file",
name: "2018-09-12.jpg",
path: "/2018-09-12.jpg",
children: []
},
{
type: "file",
name: "2020-19-03.jpg",
path: "/2020-19-03.jpg",
children: []
}
]
},
{
type: "folder",
name: "Music",
path: "/music",
children: [
{
type: "folder",
name: "hiphop",
path: "/hiphop",
children: [
{
type: "file",
name: "music-hiphop.mp3",
path: "/music-hiphop.mp3",
children: []
}
]
},
{
type: "folder",
name: "classical",
path: "/classical",
children: [
{
type: "file",
name: "music-classical-1.mp3",
path: "/music-classical-1.mp3",
children: []
},
{
type: "file",
name: "music-classical-2.mp3",
path: "/music-classical-2.mp3",
children: []
},
{
type: "file",
name: "music-classical-3.mp3",
path: "/music-classical-3.mp3",
children: []
}
]
},
{
type: "folder",
name: "desi",
path: "/desi",
children: [
{
type: "file",
name: "music-desi-1.mp3",
path: "/music-desi-1.mp3",
children: []
}
]
}
]
}
]
}
];
const recursive = function(dir, index) {
let str = " ".repeat(index) + "├── " + dir.name;
index += 4;
str += `
`;
for (const folder of dir.children) {
str += constructDirectory(folder, index);
}
return str;
};
const constructDirectory = function(root, index) {
if (root && root.type === "file") {
return " ".repeat(index) + "├──" + root.name + "\n\t";
}
return recursive(root, index);
};
console.log(constructDirectory(
directoryRoot.pop(),
0));