mirror of
https://github.com/nicotsx/zerobyte.git
synced 2026-04-19 22:37:14 -04:00
* refactor: strip out volume path in snapshot list / restore chore: lint issue * test: backups new include patterns
26 lines
688 B
TypeScript
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("/");
|
|
};
|