-
Notifications
You must be signed in to change notification settings - Fork 3
/
pathutils.js
53 lines (41 loc) · 1.43 KB
/
pathutils.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
const protoRegex = /^(file:\/\/|ddb:\/\/|ddb+unsafe:\/\/)/i;
module.exports = {
basename: function (path) {
const pathWithoutProto = path.replace(protoRegex, "");
const name = pathWithoutProto.split(/[\\/]/).pop();
if (name) return name;
else return pathWithoutProto;
},
join: function (...paths) {
return paths.map(p => {
if (p[p.length - 1] === "/") return p.slice(0, p.length - 1);
else return p;
}).join("/");
},
getParentFolder: function (path) {
if (typeof path === 'undefined' || path == null)
throw "Path is required";
var idx = path.lastIndexOf('/');
if (idx == -1) return null;
return path.substr(0, idx);
},
getTree: function (path) {
path = path.replace("file://", "");
var folders = [];
var f = path;
do {
folders.push(this.removeTrailingSlash(f));
f = this.getParentFolder(f);
} while (f != null);
return folders.reverse();
},
localPathFromUri: function(uri){
if (!uri.startsWith("file://")) throw new Error("Not a local URI");
let p = uri.replace("file://", "");
return this.removeTrailingSlash(p);
},
removeTrailingSlash: function(path){
if (path.length > 1 && path[path.length - 1] === '/') path = path.substring(0, path.length - 1);
return path;
}
}