mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Add systemOfMeasurement to VariantUnitManager
This commit is contained in:
@@ -7,33 +7,41 @@ export default class VariantUnitManager {
|
||||
|
||||
getUnitName(scale, unitType) {
|
||||
if (this.units[unitType][scale]) {
|
||||
return this.units[unitType][scale]['name'];
|
||||
return this.units[unitType][scale]["name"];
|
||||
} else {
|
||||
return '';
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Filter by measurement system
|
||||
// Filter by measurement system
|
||||
compatibleUnitScales(scale, unitType) {
|
||||
const scaleSystem = this.units[unitType][scale]['system'];
|
||||
const scaleSystem = this.units[unitType][scale]["system"];
|
||||
|
||||
return Object.entries(this.units[unitType])
|
||||
.filter(([scale, scaleInfo]) => {
|
||||
return scaleInfo['system'] == scaleSystem;
|
||||
return scaleInfo["system"] == scaleSystem;
|
||||
})
|
||||
.map(([scale, _]) => parseFloat(scale))
|
||||
.sort();
|
||||
}
|
||||
|
||||
systemOfMeasurement(scale, unitType) {
|
||||
if (this.units[unitType][scale]) {
|
||||
return this.units[unitType][scale]["system"];
|
||||
} else {
|
||||
return "custom";
|
||||
}
|
||||
}
|
||||
|
||||
// private
|
||||
|
||||
#loadUnits(units) {
|
||||
// Transform unit scale to a JS Number for compatibility. This would be way simpler in Ruby or Coffeescript!!
|
||||
const unitsTransformed = Object.entries(units).map(([measurement, measurementInfo]) => {
|
||||
const measurementInfoTransformed = Object.fromEntries(Object.entries(measurementInfo).map(([scale, unitInfo]) =>
|
||||
[ parseFloat(scale), unitInfo ]
|
||||
));
|
||||
return [ measurement, measurementInfoTransformed ];
|
||||
const measurementInfoTransformed = Object.fromEntries(
|
||||
Object.entries(measurementInfo).map(([scale, unitInfo]) => [parseFloat(scale), unitInfo]),
|
||||
);
|
||||
return [measurement, measurementInfoTransformed];
|
||||
});
|
||||
return Object.fromEntries(unitsTransformed);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user