Normalize sidebar units and record registry updates
This commit is contained in:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user