rvx
Version:
A signal based rendering library
51 lines • 1.4 kB
JavaScript
export function normalize(path, preserveDir = true) {
if (path === "/" || path === "") {
return "";
}
if (!preserveDir && path.endsWith("/")) {
path = path.slice(0, path.length - 1);
}
if (path.startsWith("/")) {
return path;
}
return "/" + path;
}
export function join(parent, child, preserveDir = true) {
child = normalize(child, preserveDir);
parent = normalize(parent, child === "" ? preserveDir : false);
return parent + child;
}
export function relative(from, to, preserveDir = true) {
const base = normalize(from, false);
to = normalize(to, preserveDir);
if (base.length === 0) {
return to;
}
let basePos = 0;
for (;;) {
const sep = base.indexOf("/", basePos + 1);
const end = sep < 0 ? base.length : sep;
const part = base.slice(basePos, end);
if (to === part || (to.startsWith(part, basePos) && to[basePos + part.length] === "/")) {
basePos = end;
}
else {
break;
}
if (sep < 0) {
break;
}
}
let back = 0;
for (let i = basePos; i < base.length; i++) {
if (base[i] === "/") {
back++;
}
}
to = to.slice(basePos);
if (back === 0 && to === "/") {
return "";
}
return "/..".repeat(back) + to;
}
//# sourceMappingURL=path.js.map