refactor(web): extract groupDefinitions helper; object-detail uses it (#45)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-08 13:58:12 +02:00
parent c0c86a5859
commit a9a0c4d477
3 changed files with 74 additions and 13 deletions
+40
View File
@@ -0,0 +1,40 @@
import { expect, test } from "vitest";
import { groupDefinitions } from "./group-fields";
import type { components } from "../api/schema";
type FieldDefinitionView = components["schemas"]["FieldDefinitionView"];
type MinDef = Pick<FieldDefinitionView, "key" | "group">;
const def = (key: string, group: string | null): MinDef => ({
key,
group: group as FieldDefinitionView["group"],
});
function keysByGroup(defs: MinDef[]) {
return groupDefinitions(defs as FieldDefinitionView[], "Other").map((g) => ({
group: g.group,
keys: g.defs.map((d) => d.key),
}));
}
test("preserves definition order within and across groups; Other is last", () => {
const result = keysByGroup([
def("a", "Description"),
def("b", null),
def("c", "Description"),
def("d", "Provenance"),
def("e", " "),
]);
expect(result).toEqual([
{ group: "Description", keys: ["a", "c"] },
{ group: "Provenance", keys: ["d"] },
{ group: "Other", keys: ["b", "e"] },
]);
});
test("all-ungrouped → a single trailing Other group", () => {
expect(keysByGroup([def("x", null), def("y", null)])).toEqual([
{ group: "Other", keys: ["x", "y"] },
]);
});
+30
View File
@@ -0,0 +1,30 @@
import type { components } from "../api/schema";
type FieldDefinitionView = components["schemas"]["FieldDefinitionView"];
export type FieldGroup = { group: string; defs: FieldDefinitionView[] };
/** Group field definitions by `def.group` (trimmed), preserving definition order
* within and across groups; ungrouped defs fall into a trailing `otherLabel` bucket. */
export function groupDefinitions(
definitions: FieldDefinitionView[],
otherLabel: string,
): FieldGroup[] {
const groups: FieldGroup[] = [];
for (const def of definitions) {
const group = def.group?.trim() ? def.group : otherLabel;
let bucket = groups.find((g) => g.group === group);
if (!bucket) {
bucket = { group, defs: [] };
groups.push(bucket);
}
bucket.defs.push(def);
}
groups.sort((a, b) => Number(a.group === otherLabel) - Number(b.group === otherLabel));
return groups;
}