Skip to content

Commit

Permalink
Factor out convertTemperature
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCBrammer committed Sep 3, 2023
1 parent 1c6ed57 commit 88ba833
Showing 1 changed file with 31 additions and 51 deletions.
82 changes: 31 additions & 51 deletions app/packs/src/models/Reaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ const TemperatureDefault = {
data: []
};

export const convertTemperature = (temperature, fromUnit, toUnit) => {
const conversionTable = {
'K': {
'°C': (t) => parseFloat(t) - 273.15,
'°F': (t) => (parseFloat(t) * 9 / 5) - 459.67
},
'°C': {
'K': (t) => parseFloat(t) + 273.15,
'°F': (t) => (parseFloat(t) * 1.8) + 32
},
'°F': {
'K': (t) => (parseFloat(t) + 459.67) * 5 / 9,
'°C': (t) => (parseFloat(t) - 32) / 1.8
}
};
return conversionTable[fromUnit][toUnit](temperature);
};

const MomentUnit = {
'Week(s)': 'weeks',
'Day(s)': 'days',
Expand Down Expand Up @@ -58,7 +76,7 @@ const DurationDefault = {
memUnit: 'Hour(s)'
};

const convertDuration = (value, unit, newUnit) => {
export const convertDuration = (value, unit, newUnit) => {
const d = moment.duration(Number.parseFloat(value), LegMomentUnit[unit])
.as(MomentUnit[newUnit]);
return round(d, 1).toString();
Expand Down Expand Up @@ -362,60 +380,22 @@ export default class Reaction extends Element {
}

convertTemperature(newUnit) {
let temperature = this._temperature
let oldUnit = temperature.valueUnit
temperature.valueUnit = newUnit

let convertFunc
switch (oldUnit) {
case "K":
convertFunc = this.convertFromKelvin
break
case "°F":
convertFunc = this.convertFromFarenheit
break
default:
convertFunc = this.convertFromCelcius
break
}
const temperature = this._temperature
const oldUnit = temperature.valueUnit;
temperature.valueUnit = newUnit;

// If userText is number only, treat as normal temperature value
if (/^[\-|\d]\d*\.{0,1}\d{0,2}$/.test(temperature.userText)) {
temperature.userText =
convertFunc(newUnit, temperature.userText).toFixed(2)

return temperature
}

temperature.data.forEach(function (data, index, theArray) {
theArray[index].value = convertFunc(newUnit, data.value).toFixed(2)
})

return temperature
}
temperature.userText = convertTemperature(temperature.userText, oldUnit, newUnit).toFixed(2);

convertFromKelvin(unit, temperature) {
if (unit == "°C") {
return (parseFloat(temperature) - 273.15)
} else { // Farenheit
return ((parseFloat(temperature) * 9 / 5) - 459.67)
return temperature;
}
}

convertFromFarenheit(unit, temperature) {
if (unit == "°C") {
return ((parseFloat(temperature) - 32) / 1.8)
} else { // Kelvin
return ((parseFloat(temperature) + 459.67) * 5 / 9)
}
}
temperature.data.forEach((data, index, theArray) => {
theArray[index].value = convertTemperature(data.value, oldUnit, newUnit).toFixed(2);
});

convertFromCelcius(unit, temperature) {
if (unit == "°F") {
return ((parseFloat(temperature) * 1.8) + 32)
} else { // Kelvin
return (parseFloat(temperature) + 273.15)
}
return temperature;
}

get short_label() {
Expand Down Expand Up @@ -876,9 +856,9 @@ export default class Reaction extends Element {
get totalVolume() {
let totalVolume = 0.0;
const materials = [...this.starting_materials,
...this.reactants,
...this.products,
...this.solvents];
...this.reactants,
...this.products,
...this.solvents];
materials.map(m => totalVolume += m.amount_l);
return totalVolume;
}
Expand Down

0 comments on commit 88ba833

Please sign in to comment.