1
0
silverbullet/common/path.ts

24 lines
569 B
TypeScript
Raw Normal View History

2023-07-07 11:09:44 +00:00
export function folderName(path: string) {
return path.split("/").slice(0, -1).join("/");
}
export function resolve(...paths: string[]) {
const parts = paths.reduce((acc, path) => {
return acc.concat(path.split("/"));
}, [] as string[]);
const resolvedParts = [];
for (const part of parts) {
if (part === "..") {
resolvedParts.pop();
} else if (part !== ".") {
resolvedParts.push(part);
}
}
const result = resolvedParts.join("/");
if (result[0] === "/") {
return result.substring(1);
} else {
return result;
}
}