-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.js
More file actions
47 lines (41 loc) · 1.06 KB
/
FileSystem.js
File metadata and controls
47 lines (41 loc) · 1.06 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
const paths = {
currentDir: fileSystem["~"],
parsedPath: fileSystem["~"],
currentPath: "~"
};
function pathParser(path) {
let parts = path.split('/').filter(p => p.length > 0);
let parsedPath = [];
let dir = fileSystem;
if (parts[0] !== '~')
parsedPath = paths.currentPath.split('/').filter(p => p.length > 0);
for (let part of parts) {
if (part === '.')
continue;
else if (part === '..')
parsedPath.pop();
else
parsedPath.push(part);
}
if (!parsedPath.length)
return "~";
for (let part of parsedPath) {
if (dir[part])
dir = dir[part];
else
return "";
}
return parsedPath.join('/');
}
function getDirectory(path) {
let parts = path.split('/').filter(p => p.length > 0);
let dir = fileSystem;
for (let part of parts) {
if (dir[part]) {
dir = dir[part];
} else {
return null;
}
}
return dir;
}