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

This commit is contained in:
Akshar Patel authored and GitHub committed 2026-09-06 14:09:26 -03:00
1 parent 72cb638a8a
commit 64fafbdfcf
2 files changed
+12 -3

No files matched your search

@@ -67,8 +67,15 @@ describe("fileBreadcrumbChildren", () => {
]);
});
it("uses natural file-name ordering", () => {
expect(fileBreadcrumbChildren(entries, "src/lib").map((entry) => entry.label)).toEqual([
it("uses natural file-name ordering and preserves input order for equivalent names", () => {
const files = ["file10.ts", "File2.ts", "file02.ts", "file2.ts"].map((name) => ({
path: `src/lib/${name}`,
kind: "file" as const,
}));
expect(fileBreadcrumbChildren(files, "src/lib").map((entry) => entry.label)).toEqual([
"File2.ts",
"file02.ts",
"file2.ts",
"file10.ts",
]);
+3 -1
View File
@@ -36,6 +36,7 @@ export function fileBreadcrumbChildren(
entries: readonly ProjectEntry[],
directoryPath: string,
): FileBreadcrumbChild[] {
let collator: Intl.Collator | undefined;
const prefix = directoryPath ? `${directoryPath}/` : "";
return entries
.flatMap((entry) => {
@@ -46,10 +47,11 @@ export function fileBreadcrumbChildren(
})
.toSorted((left, right) => {
if (left.kind !== right.kind) return left.kind === "directory" ? -1 : 1;
return left.label.localeCompare(right.label, undefined, {
collator ??= new Intl.Collator(undefined, {
numeric: true,
sensitivity: "base",
});
return collator.compare(left.label, right.label);
});
}