-
Notifications
You must be signed in to change notification settings - Fork 202
/
path.ts
33 lines (31 loc) · 874 Bytes
/
path.ts
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
/** @hidden
* Removes leading and trailing slashes from a path.
*
* @param path A path to normalize, in POSIX format.
*/
export function normalizePath(path: string): string {
if (!path) {
return "";
}
return path.replace(/^\//, "").replace(/\/$/, "");
}
/**
* Normalizes a given path and splits it into an array of segments.
*
* @param path A path to split, in POSIX format.
*/
export function pathParts(path: string): string[] {
if (!path || path === "" || path === "/") {
return [];
}
return normalizePath(path).split("/");
}
/**
* Normalizes given paths and joins these together using a POSIX separator.
*
* @param base A first path segment, in POSIX format.
* @param child A second path segment, in POSIX format.
*/
export function joinPath(base: string, child: string) {
return pathParts(base).concat(pathParts(child)).join("/");
}