-
Notifications
You must be signed in to change notification settings - Fork 29
/
tempConversion.js
165 lines (146 loc) · 4.53 KB
/
tempConversion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Returns the input value as is.
* @param {number} value - The input value.
* @returns {number} The input value.
*/
function identityFunction(value) {
return value;
}
/**
* Converts Celsius to Fahrenheit.
* @param {number} celsius - The temperature in Celsius.
* @returns {number} The temperature in Fahrenheit.
*/
function celsiusToFahrenheit(celsius) {
return (celsius * 9/5) + 32;
}
/**
* Converts Celsius to Kelvin.
* @param {number} celsius - The temperature in Celsius.
* @returns {number} The temperature in Kelvin.
*/
function celsiusToKelvin(celsius) {
return celsius + 273.15;
}
/**
* Converts Celsius to Rankine.
* @param {number} celsius - The temperature in Celsius.
* @returns {number} The temperature in Rankine.
*/
function celsiusToRankine(celsius) {
return (celsius * 9/5) + 491.67;
}
/**
* Converts Fahrenheit to Celsius.
* @param {number} fahrenheit - The temperature in Fahrenheit.
* @returns {number} The temperature in Celsius.
*/
function fahrenheitToCelsius(fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
/**
* Converts Fahrenheit to Kelvin.
* @param {number} fahrenheit - The temperature in Fahrenheit.
* @returns {number} The temperature in Kelvin.
*/
function fahrenheitToKelvin(fahrenheit) {
return (fahrenheit - 32) * 5/9 + 273.15;
}
/**
* Converts Fahrenheit to Rankine.
* @param {number} fahrenheit - The temperature in Fahrenheit.
* @returns {number} The temperature in Rankine.
*/
function fahrenheitToRankine(fahrenheit) {
return fahrenheit + 459.67;
}
/**
* Converts Kelvin to Celsius.
* @param {number} kelvin - The temperature in Kelvin.
* @returns {number} The temperature in Celsius.
*/
function kelvinToCelsius(kelvin) {
return kelvin - 273.15;
}
/**
* Converts Kelvin to Fahrenheit.
* @param {number} kelvin - The temperature in Kelvin.
* @returns {number} The temperature in Fahrenheit.
*/
function kelvinToFahrenheit(kelvin) {
return (kelvin - 273.15) * 9/5 + 32;
}
/**
* Converts Kelvin to Rankine.
* @param {number} kelvin - The temperature in Kelvin.
* @returns {number} The temperature in Rankine.
*/
function kelvinToRankine(kelvin) {
return kelvin * 9/5;
}
/**
* Converts Rankine to Celsius.
* @param {number} rankine - The temperature in Rankine.
* @returns {number} The temperature in Celsius.
*/
function rankineToCelsius(rankine) {
return (rankine - 491.67) * 5/9;
}
/**
* Converts Rankine to Fahrenheit.
* @param {number} rankine - The temperature in Rankine.
* @returns {number} The temperature in Fahrenheit.
*/
function rankineToFahrenheit(rankine) {
return rankine - 459.67;
}
/**
* Converts Rankine to Kelvin.
* @param {number} rankine - The temperature in Rankine.
* @returns {number} The temperature in Kelvin.
*/
function rankineToKelvin(rankine) {
return rankine * 5/9;
}
/**
* Returns the temperature conversion function for the given input and output temperature types.
* @param {string} fromTemp - The input temperature type.
* @param {string} toTemp - The output temperature type.
* @returns {function} The temperature conversion function.
*/
function tempFunctionMap(fromTemp, toTemp) {
const tempFuncMap = new Map([
["CelsiusCelsius", identityFunction],
["CelsiusFahrenheit", celsiusToFahrenheit],
["CelsiusKelvin", celsiusToKelvin],
["CelsiusRankine", celsiusToRankine],
["FahrenheitCelsius", fahrenheitToCelsius],
["FahrenheitFahrenheit", identityFunction],
["FahrenheitKelvin", fahrenheitToKelvin],
["FahrenheitRankine", fahrenheitToRankine],
["KelvinCelsius", kelvinToCelsius],
["KelvinFahrenheit", kelvinToFahrenheit],
["KelvinKelvin", identityFunction],
["KelvinRankine", kelvinToRankine],
["RankineCelsius", rankineToCelsius],
["RankineFahrenheit", rankineToFahrenheit],
["RankineKelvin", rankineToKelvin],
["RankineRankine", identityFunction],
]);
const key = fromTemp + toTemp;
return tempFuncMap.get(key);
}
/**
* Returns the temperature unit symbol for the given temperature type.
* @param {string} tempType - The temperature type.
* @returns {string} The temperature unit symbol.
*/
function tempUnitMap(tempType) {
const tempUnitMap = new Map([
["Celsius", "°C"],
["Fahrenheit", "°F"],
["Kelvin", "K"],
["Rankine", "°R"],
]);
return tempUnitMap.get(tempType)
}