38 lines
953 B
JavaScript
38 lines
953 B
JavaScript
|
|
//#region src/common/utilities/path.ts
|
|
/** @hidden
|
|
* Removes leading and trailing slashes from a path.
|
|
*
|
|
* @param path A path to normalize, in POSIX format.
|
|
*/
|
|
function normalizePath(path) {
|
|
if (!path) {
|
|
return "";
|
|
}
|
|
return path.replace(/^\//, "").replace(/\/$/, "").replace(/\/{2,}/g, "/");
|
|
}
|
|
/**
|
|
* Normalizes a given path and splits it into an array of segments.
|
|
*
|
|
* @param path A path to split, in POSIX format.
|
|
*/
|
|
function pathParts(path) {
|
|
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.
|
|
*/
|
|
function joinPath(base, child) {
|
|
return pathParts(base).concat(pathParts(child)).join("/");
|
|
}
|
|
|
|
//#endregion
|
|
exports.joinPath = joinPath;
|
|
exports.normalizePath = normalizePath;
|
|
exports.pathParts = pathParts; |