Normalize sidebar units and record registry updates

This commit is contained in:
Codex
2026-03-07 09:55:04 +00:00
parent 6ae2c38b61
commit 1d4a0bdba9
3 changed files with 102 additions and 13 deletions

View File

@@ -12,26 +12,35 @@
conversions: CalculatorDef[];
};
type UnitBucket = {
label: string;
conversions: CalculatorDef[];
};
$: categoryUnitGroups = Object.entries(categories).map(([key, meta]) => {
const buckets = new Map<string, CalculatorDef[]>();
const buckets = new Map<string, UnitBucket>();
const calcs = getCalculatorsByCategory(key);
calcs.forEach(calc => {
[calc.labels.in1, calc.labels.in2].forEach(unit => {
const existing = buckets.get(unit);
const key = unit.toLowerCase();
const existing = buckets.get(key);
if (existing) {
existing.push(calc);
existing.conversions.push(calc);
} else {
buckets.set(unit, [calc]);
buckets.set(key, {
label: unit,
conversions: [calc],
});
}
});
});
const units = [...buckets.entries()]
.sort(([a], [b]) => a.localeCompare(b))
.map(([label, conversions]) => ({
label,
conversions: conversions.slice().sort((a, b) => a.name.localeCompare(b.name)),
.map(([, bucket]) => ({
label: bucket.label,
conversions: bucket.conversions.slice().sort((a, b) => a.name.localeCompare(b.name)),
}));
return { key, meta, units };