Skip to content

Commit 9faa7b4

Browse files
authored
fix: lcp on directory prefix (#190)
1 parent dcfd8e0 commit 9faa7b4

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

src/kicanvas/services/vfs.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,20 +352,29 @@ export class DragAndDropFileSystem extends LocalFileSystemBase {
352352
}
353353

354354
private static lcp(str: string[]): string {
355-
const beg = str[0] ?? "";
356-
return str.reduce((common, path) => {
357-
let i = 0;
355+
if (str.length === 0) {
356+
return "";
357+
}
358358

359-
while (
360-
i < common.length &&
361-
i < path.length &&
362-
common[i] === path[i]
363-
) {
359+
// split dirname only
360+
const str_arr = str.map((s) =>
361+
s.split("/").filter(Boolean).slice(0, -1),
362+
);
363+
364+
const fst = str_arr[0]!;
365+
let p_len = fst.length;
366+
for (const s of str_arr.slice(1)) {
367+
let i = 0;
368+
while (i < p_len && i < s.length && fst[i] === s[i]) {
364369
i += 1;
365370
}
371+
p_len = i;
372+
if (p_len === 0) {
373+
break;
374+
}
375+
}
366376

367-
return common.slice(0, i);
368-
}, beg);
377+
return fst.slice(0, p_len).join("/");
369378
}
370379

371380
private static async load(entry: FileSystemFileEntry): Promise<File> {

0 commit comments

Comments
 (0)