forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilePath.js
38 lines (35 loc) · 1 KB
/
filePath.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
export function resolvePathToFile(filePath, files) {
if (filePath === undefined) {
return false;
}
const filePathArray = filePath.split('/');
let resolvedFile;
let currentFile = files.find((file) => file.name === 'root');
filePathArray.some((filePathSegment, index) => {
if (filePathSegment === '' || filePathSegment === '.') {
return false;
} else if (filePathSegment === '..') {
return true;
}
let foundChild = false;
const childFiles = currentFile.children.map((childFileId) =>
files.find(
(file) => file._id.valueOf().toString() === childFileId.valueOf()
)
);
childFiles.some((childFile) => {
if (childFile.name === filePathSegment) {
currentFile = childFile;
foundChild = true;
if (index === filePathArray.length - 1) {
resolvedFile = childFile;
}
return true;
}
return false;
});
return !foundChild;
});
return resolvedFile;
}
export default resolvePathToFile;