Currently, I am working on an Angular 7 temperature conversion application. Within my formGroup, there are inputs and outputs along with two multi-select dropdowns where users can choose the unit of temperature 'From' and 'To' for conversion (Celsius, Fahrenheit, or Kelvin).
Upon submitting the form in my component, I retrieve the input data using the following function:
submitForm(data) {
this.input = data.input;
this.unitInput= data.unitInput;
this.unitTarget= data.unitTarget;
this.output= data.output;
}
Initially, I was planning to use multiple `if` statements to determine which conversion function to call based on the selected units. However, having a long list of `if` statements seemed inefficient and poorly designed.
if (this.inputUnit === 'Celsius' && this.targetUnit === 'Fahrenheit') {
this.celsiusToFahrenheitConversion();
}
if (this.inputUnit === 'Celsius' && this.targetUnit === 'Kelvin') {
this.celsiusToKelvinConversion();
}
if (this.inputUnit === 'Celsius' && this.targetUnit === 'Rankine') {
this.celsiusToRankineConversion();
}
This approach led to branching `if` blocks for different unit conversions like Fahrenheit, Kelvin, Rankine, and more. What if new types of units need to be added in the future? This made me reconsider my strategy.
If you have any suggestions or better approaches, I would appreciate your insights. Thank you!