a9a0c4d477
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
31 lines
903 B
TypeScript
31 lines
903 B
TypeScript
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;
|
|
}
|