zerobyte/app/utils/common-ancestor.ts
Nico ca8248b2a0
ui: scroll to error (#541)
* refactor: scroll to first error when submitting a form

* refactor: split file browsers into dedicated components with base

* chore: pr feedbacks
2026-02-18 20:13:09 +01:00

19 lines
567 B
TypeScript

export const findCommonAncestor = (paths: string[]): string => {
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("/");
};