Files
zerobyte/app/utils/common-ancestor.ts
Nico 8fcd446926 refactor: snapshot strip out base path (#542)
* refactor: strip out volume path in snapshot list / restore

chore: lint issue

* test: backups new include patterns
2026-02-18 21:45:19 +01:00

26 lines
688 B
TypeScript

export const findCommonAncestor = (paths: string[]): string => {
for (const p of paths) {
if (!p.startsWith("/")) {
throw new Error(`Path "${p}" is not absolute.`);
}
}
if (paths.length === 0) return "/";
if (paths.length === 1) return paths[0] || "/";
const splitPaths = paths.map((path) => path.split("/").filter(Boolean));
const minLength = Math.min(...splitPaths.map((parts) => parts.length));
const commonParts: string[] = [];
for (let i = 0; i < minLength; i++) {
const partSet = new Set(splitPaths.map((parts) => parts[i]));
if (partSet.size === 1) {
commonParts.push(splitPaths[0][i]);
} else {
break;
}
}
return "/" + commonParts.join("/");
};