mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-06 04:21:55 +00:00
44 lines
919 B
TypeScript
44 lines
919 B
TypeScript
import slash from "slash";
|
|
|
|
/**
|
|
* Matches:
|
|
* - ftp://
|
|
* - https://
|
|
* - //
|
|
*/
|
|
export const absolutePathRegex = /^(?:[a-z]+:)?\/\//;
|
|
|
|
export const isRelativePath = (str: string) => {
|
|
const isAbsolute = absolutePathRegex.exec(str);
|
|
if (isAbsolute) return false;
|
|
return true;
|
|
};
|
|
|
|
const pathJoin = function (...pathArr) {
|
|
return pathArr
|
|
.map(function (path) {
|
|
if (path[0] === "/") {
|
|
path = path.slice(1);
|
|
}
|
|
if (path.startsWith("./")) {
|
|
path = path.slice(2);
|
|
}
|
|
if (path[path.length - 1] === "/") {
|
|
path = path.slice(0, path.length - 1);
|
|
}
|
|
return path;
|
|
})
|
|
.join("/");
|
|
};
|
|
|
|
export const getFullRelativePath = (...paths: string[]) => {
|
|
return isRelativePath(paths[paths.length - 1])
|
|
? slash(pathJoin(...paths))
|
|
: paths[paths.length - 1];
|
|
};
|
|
|
|
export const trimTrailingSlash = (path: string) => {
|
|
if (path.endsWith("/")) return path.slice(0, path.length - 1);
|
|
return path;
|
|
};
|