Sidebar updates
This commit is contained in:
@@ -31,42 +31,32 @@
|
|||||||
conversions: CalculatorDef[];
|
conversions: CalculatorDef[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const sortConversionsForUnit = (conversions: CalculatorDef[], unitLabel: string) => {
|
const sortConversionsForUnit = (conversions: CalculatorDef[]) =>
|
||||||
const normalizedUnit = unitLabel.toLowerCase();
|
conversions.slice().sort((a, b) => a.name.localeCompare(b.name));
|
||||||
return conversions.slice().sort((a, b) => {
|
|
||||||
const aIsSource = a.labels.in1?.toLowerCase() === normalizedUnit;
|
|
||||||
const bIsSource = b.labels.in1?.toLowerCase() === normalizedUnit;
|
|
||||||
if (aIsSource !== bIsSource) {
|
|
||||||
return aIsSource ? -1 : 1;
|
|
||||||
}
|
|
||||||
return a.name.localeCompare(b.name);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
$: categoryUnitGroups = Object.entries(categories).map(([key, meta]) => {
|
$: categoryUnitGroups = Object.entries(categories).map(([key, meta]) => {
|
||||||
const buckets = new Map<string, UnitBucket>();
|
const buckets = new Map<string, UnitBucket>();
|
||||||
const calcs = getCalculatorsByCategory(key);
|
const calcs = getCalculatorsByCategory(key);
|
||||||
|
|
||||||
calcs.forEach(calc => {
|
calcs.forEach(calc => {
|
||||||
[calc.labels.in1, calc.labels.in2].forEach(unit => {
|
const unit = calc.labels.in1;
|
||||||
const key = unit.toLowerCase();
|
const bucketKey = unit.toLowerCase();
|
||||||
const existing = buckets.get(key);
|
const existing = buckets.get(bucketKey);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
existing.conversions.push(calc);
|
existing.conversions.push(calc);
|
||||||
} else {
|
} else {
|
||||||
buckets.set(key, {
|
buckets.set(bucketKey, {
|
||||||
label: unit,
|
label: unit,
|
||||||
conversions: [calc],
|
conversions: [calc],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
const units = [...buckets.entries()]
|
const units = [...buckets.entries()]
|
||||||
.sort(([a], [b]) => a.localeCompare(b))
|
.sort(([a], [b]) => a.localeCompare(b))
|
||||||
.map(([, bucket]) => ({
|
.map(([, bucket]) => ({
|
||||||
label: bucket.label,
|
label: bucket.label,
|
||||||
conversions: sortConversionsForUnit(bucket.conversions, bucket.label),
|
conversions: sortConversionsForUnit(bucket.conversions),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return { key, meta, units };
|
return { key, meta, units };
|
||||||
|
|||||||
Reference in New Issue
Block a user