History

perf(web): speed up folder menu sorting (#10190)

Akshar Patel committed 2 months ago

Commit 64fafbd1 parent 72cb6382 files · +12 −3Browse files

Unified · Split

2 files
  1. apps/web/src/components/files/filePath.test.ts +9 −2
  2. apps/web/src/components/files/filePath.ts +3 −1
apps/web/src/components/files/filePath.test.ts+9−2
@@ -67,8 +67,15 @@ describe("fileBreadcrumbChildren", () => { · 66 unmodified lines
67 ]);67 ]);
68 });68 });
69 69
70 it("uses natural file-name ordering", () => {70 it("uses natural file-name ordering and preserves input order for equivalent names", () => {
71 expect(fileBreadcrumbChildren(entries, "src/lib").map((entry) => entry.label)).toEqual([71 const files = ["file10.ts", "File2.ts", "file02.ts", "file2.ts"].map((name) => ({
72 path: `src/lib/${name}`,
73 kind: "file" as const,
74 }));
75
76 expect(fileBreadcrumbChildren(files, "src/lib").map((entry) => entry.label)).toEqual([
77 "File2.ts",
78 "file02.ts",
72 "file2.ts",79 "file2.ts",
73 "file10.ts",80 "file10.ts",
74 ]);81 ]);
apps/web/src/components/files/filePath.ts+3−1
@@ -36,6 +36,7 @@ export function fileBreadcrumbChildren( · 35 unmodified lines
36 entries: readonly ProjectEntry[],36 entries: readonly ProjectEntry[],
37 directoryPath: string,37 directoryPath: string,
38): FileBreadcrumbChild[] {38): FileBreadcrumbChild[] {
39 let collator: Intl.Collator | undefined;
39 const prefix = directoryPath ? `${directoryPath}/` : "";40 const prefix = directoryPath ? `${directoryPath}/` : "";
40 return entries41 return entries
41 .flatMap((entry) => {42 .flatMap((entry) => {
@@ -46,10 +47,11 @@ export function fileBreadcrumbChildren( · 4 unmodified lines
46 })47 })
47 .toSorted((left, right) => {48 .toSorted((left, right) => {
48 if (left.kind !== right.kind) return left.kind === "directory" ? -1 : 1;49 if (left.kind !== right.kind) return left.kind === "directory" ? -1 : 1;
49 return left.label.localeCompare(right.label, undefined, {50 collator ??= new Intl.Collator(undefined, {
50 numeric: true,51 numeric: true,
51 sensitivity: "base",52 sensitivity: "base",
52 });53 });
54 return collator.compare(left.label, right.label);
53 });55 });
54}56}
55 57