Add volume units

pull/1496/head
Piero Toffanin 2024-05-09 12:35:29 -04:00
rodzic adf9c7dc5f
commit 681482983c
2 zmienionych plików z 59 dodań i 0 usunięć

Wyświetl plik

@ -80,6 +80,21 @@ const units = {
factor: Math.pow((3937 / 1200) / 5280, 2),
abbr: 'mi² (US)',
round: 5
},
cbmeters:{
factor: 1,
abbr: 'm³',
round: 4
},
cbyards:{
factor: Math.pow(1/(0.3048*3), 3),
abbr: 'yd³',
round: 4
},
cbyards_us:{
factor: Math.pow(3937/3600, 3),
abbr: 'yd³ (US)',
round: 4
}
};
@ -105,6 +120,8 @@ class ValueUnit{
class UnitSystem{
lengthUnit(meters){ throw new Error("Not implemented"); }
areaUnit(sqmeters){ throw new Error("Not implemented"); }
volumeUnit(cbmeters){ throw new Error("Not implemented"); }
getName(){ throw new Error("Not implemented"); }
area(sqmeters){
@ -118,6 +135,12 @@ class UnitSystem{
const val = unit.factor * meters;
return new ValueUnit(val, unit);
}
volume(cbmeters){
const unit = this.volumeUnit(cbmeters);
const val = unit.factor * cbmeters;
return new ValueUnit(val, unit);
}
};
class MetricSystem extends UnitSystem{
@ -136,6 +159,10 @@ class MetricSystem extends UnitSystem{
else if (sqmeters >= 1000000) return units.sqkilometers;
return units.sqmeters;
}
volumeUnit(cbmeters){
return units.cbmeters;
}
}
class ImperialSystem extends UnitSystem{
@ -162,6 +189,10 @@ class ImperialSystem extends UnitSystem{
acres(){
return units.acres;
}
cbyards(){
return units.cbyards;
}
lengthUnit(meters){
const feet = this.feet().factor * meters;
@ -175,6 +206,10 @@ class ImperialSystem extends UnitSystem{
else if (sqfeet >= 27878400) return this.sqmiles();
else return this.sqfeet();
}
volumeUnit(cbmeters){
return this.cbyards();
}
}
class ImperialUSSystem extends ImperialSystem{
@ -201,6 +236,10 @@ class ImperialUSSystem extends ImperialSystem{
acres(){
return units.acres_us;
}
cbyards(){
return units.cbyards_us;
}
}
const systems = {

Wyświetl plik

@ -38,6 +38,16 @@ describe('Metric system', () => {
areas.forEach(a => {
expect(metric.area(a[0]).toString()).toBe(a[1]);
});
const volumes = [
[1, "1 m³"],
[9000, "9,000 m³"],
[9000.25559, "9,000.2556 m³"],
];
volumes.forEach(v => {
expect(metric.volume(v[0]).toString()).toBe(v[1]);
});
})
});
@ -74,5 +84,15 @@ describe('Imperial systems', () => {
expect(imperial.area(a[0]).toString()).toBe(a[1]);
expect(imperialUS.area(a[0]).toString()).toBe(a[2]);
});
const volumes = [
[1, "1.308 yd³", "1.3079 yd³ (US)"],
[1000, "1,307.9506 yd³", "1,307.9428 yd³ (US)"]
];
volumes.forEach(v => {
expect(imperial.volume(v[0]).toString()).toBe(v[1]);
expect(imperialUS.volume(v[0]).toString()).toBe(v[2]);
});
})
});