Skip to content

Commit b785502

Browse files
authored
Create nested-directory.js
1 parent f2c84ca commit b785502

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

nested-directory.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const appendFilePath = (directory, parentDirectory = "") => {
2+
const { contents: directoryContents, name: directoryName } = directory;
3+
return directoryContents.map(item => {
4+
const { name, type } = item;
5+
return type === "directory"
6+
? { ...item, contents : appendFilePath(item, `${parentDirectory}/${name}`) }
7+
: { ...item, path: `/${parentDirectory}/${name}` };
8+
});
9+
};
10+
11+
const transformData = games => {
12+
return games.map(game => {
13+
const { type, name } = game;
14+
return type === "directory"
15+
? { ...game, contents: appendFilePath(game, name) }
16+
: { ...game, path: `/${name}` };
17+
});
18+
};
19+
20+
const games = [
21+
{
22+
type: "directory",
23+
name: "rock-the-dock",
24+
contents: [
25+
{ type: "file", name: "appmanifest.json" },
26+
{ type: "file", name: "c2runtime.js" },
27+
{ type: "file", name: "data.js" },
28+
{ type: "file", name: "favicon.png" },
29+
{ type: "file", name: "games_SDK.js" },
30+
{ type: "file", name: "gamezop.js" },
31+
{ type: "file", name: "icon-114.png" },
32+
{ type: "file", name: "icon-128.png" },
33+
{ type: "file", name: "icon-16.png" },
34+
{ type: "file", name: "icon-256.png" },
35+
{ type: "file", name: "icon-32.png" },
36+
{ type: "file", name: "icon.png" },
37+
{
38+
type: "directory",
39+
name: "icons",
40+
contents: [
41+
{ type: "file", name: "icon16x16.png" },
42+
{ type: "file", name: "icon48x48.png" },
43+
{ type: "file", name: "icon60x60.png" },
44+
{ type: "file", name: "RocktheDock_128.png" }
45+
]
46+
},
47+
48+
{
49+
type: "directory",
50+
name: "images",
51+
contents: [
52+
{ type: "file", name: "base-sheet0.png" },
53+
{ type: "file", name: "bg-sheet0.png" },
54+
{ type: "file", name: "blank-sheet0.png" },
55+
{ type: "file", name: "buttons2-sheet0.png" },
56+
{ type: "file", name: "camera-sheet0.png" },
57+
{ type: "file", name: "clound-sheet0.png" },
58+
{ type: "file", name: "defaultblock-sheet0.png" },
59+
{ type: "file", name: "defaultblock-sheet1.png" },
60+
{ type: "file", name: "defaultfnt.png" },
61+
{ type: "file", name: "fin-sheet0.png" },
62+
{ type: "file", name: "gre.png" },
63+
{ type: "file", name: "loaderbg-sheet0.png" },
64+
{ type: "file", name: "logo_loadedtiled.png" },
65+
{ type: "file", name: "logo_loading-sheet0.png" },
66+
{ type: "file", name: "particles.png" },
67+
{ type: "file", name: "placementmsg-sheet0.png" },
68+
{ type: "file", name: "rotate-sheet0.png" },
69+
{ type: "file", name: "seagull-sheet0.png" },
70+
{ type: "file", name: "seagull-sheet1.png" },
71+
{ type: "file", name: "shipcabin-sheet0.png" },
72+
{ type: "file", name: "stars-sheet0.png" },
73+
{ type: "file", name: "startblock-sheet0.png" },
74+
{ type: "file", name: "sun-sheet0.png" },
75+
{ type: "file", name: "title-sheet0.png" },
76+
{ type: "file", name: "watereffect-sheet0.png" },
77+
{ type: "file", name: "water-sheet0.png" }
78+
]
79+
},
80+
{ type: "file", name: "index.html" },
81+
{ type: "file", name: "jiogames.js" },
82+
{ type: "file", name: "jquery-2.1.1.min.js" },
83+
{ type: "file", name: "loading-logo.png" },
84+
{ type: "file", name: "manifest.webapp" },
85+
{
86+
type: "directory",
87+
name: "media",
88+
contents: [
89+
{ type: "file", name: "a1_07138.m4a" },
90+
{ type: "file", name: "a1_07138.ogg" },
91+
{ type: "file", name: "a5_02038.m4a" },
92+
{ type: "file", name: "a5_02038.ogg" },
93+
{ type: "file", name: "a5_02040.m4a" },
94+
{ type: "file", name: "a5_02040.ogg" },
95+
{ type: "file", name: "a5_04117.m4a" },
96+
{ type: "file", name: "a5_04117.ogg" },
97+
{ type: "file", name: "bgloop1.m4a" },
98+
{ type: "file", name: "bgloop1.ogg" },
99+
{ type: "file", name: "star1.m4a" },
100+
{ type: "file", name: "star1.ogg" }
101+
]
102+
},
103+
{ type: "file", name: "offline.appcache" },
104+
{ type: "file", name: "offlineClient.js" },
105+
{ type: "file", name: "offline.js" },
106+
{ type: "file", name: "sw.js" }
107+
]
108+
}
109+
];
110+
111+
console.log(transformData(games));

0 commit comments

Comments
 (0)