mirror of
https://github.com/nicotsx/zerobyte.git
synced 2026-04-17 21:37:06 -04:00
* feat: export snapshot as tar file chore(mutext): prevent double lock release * chore: pr feedbacks * fix: dump single file no tar * chore: pr feedbacks
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
export const normalizeAbsolutePath = (value?: string): string => {
|
|
const trimmed = value?.trim();
|
|
if (!trimmed) return "/";
|
|
|
|
let normalizedInput: string;
|
|
try {
|
|
normalizedInput = decodeURIComponent(trimmed).replace(/\\+/g, "/");
|
|
} catch {
|
|
normalizedInput = trimmed.replace(/\\+/g, "/");
|
|
}
|
|
const withLeadingSlash = normalizedInput.startsWith("/") ? normalizedInput : `/${normalizedInput}`;
|
|
|
|
const parts = withLeadingSlash.split("/");
|
|
const stack: string[] = [];
|
|
|
|
for (const part of parts) {
|
|
if (part === "" || part === ".") {
|
|
continue;
|
|
}
|
|
if (part === "..") {
|
|
if (stack.length > 0) {
|
|
stack.pop();
|
|
}
|
|
} else {
|
|
stack.push(part);
|
|
}
|
|
}
|
|
|
|
let normalized = "/" + stack.join("/");
|
|
|
|
if (!normalized || normalized === "." || normalized.startsWith("..")) {
|
|
return "/";
|
|
}
|
|
|
|
const withoutTrailingSlash = normalized.replace(/\/+$/, "");
|
|
if (!withoutTrailingSlash) {
|
|
return "/";
|
|
}
|
|
|
|
const withSingleLeadingSlash = withoutTrailingSlash.startsWith("/")
|
|
? `/${withoutTrailingSlash.replace(/^\/+/, "")}`
|
|
: `/${withoutTrailingSlash}`;
|
|
|
|
return withSingleLeadingSlash || "/";
|
|
};
|