|
1 | 1 | /* |
2 | 2 | activation_example:30c or 30 degrees celsius |
3 | | -regex:(?:^|\s)(-?\d{1,3}\.?\d{0,2})°?\s?(?:degrees)?\s?c(?:elsius|elcius)?\b |
| 3 | +regex:(?:^|\s)(-?\d{1,3}\.?\d{0,2})°?\s?(?:degrees)?\s?(?:c(?:elsius)?|f(?:ahrenheit)?)\b |
4 | 4 | flags:gmi |
5 | 5 | */ |
6 | 6 |
|
7 | | -const regexTest = /(-?\d{1,3}(?:\.\d{1,2})?)°?\s?(?:degrees?)?\s?c(?:elsius)?\b/gi; |
8 | | -const celsiusToFahrenheit = c => ((c * 9 / 5) + 32).toFixed(2); |
9 | 7 | const formatNumber = num => Number(num).toFixed(2).replace(/\.00$/, ''); |
10 | 8 | const conversions = []; |
11 | 9 |
|
12 | | -let match; |
13 | | -while ((match = regexTest.exec(current.text)) !== null) { |
14 | | - const celsius = parseFloat(match); |
| 10 | +// convert C to F |
| 11 | +const celsiusRegex = /(-?\d{1,3}(?:\.\d{1,2})?)°?\s?(?:degrees?)?\s?c(?:elsius)?\b/gi; |
| 12 | +const celsiusToFahrenheit = c => ((c * 9 / 5) + 32).toFixed(2); |
| 13 | +let cMatch; |
| 14 | +while ((cMatch = celsiusRegex.exec(current.text)) !== null) { |
| 15 | + const celsius = parseFloat(cMatch); |
15 | 16 | const fahrenheit = celsiusToFahrenheit(celsius); |
16 | | - conversions.push(`${formatNumber(celsius)}°C is ${formatNumber(fahrenheit)} degrees in sane units (Fahrenheit).`); |
| 17 | + conversions.push(`${formatNumber(celsius)}°C is ${formatNumber(fahrenheit)} degrees in freedom units (Fahrenheit).`); |
17 | 18 | } |
18 | 19 |
|
| 20 | +// convert F to C |
| 21 | +const fahrenheitRegex = /(-?\d{1,3}(?:\.\d{1,2})?)°?\s?(?:degrees?)?\s?f(?:ahrenheit)?\b/gi; |
| 22 | +const fahrenheitToCelsius = f => ((f - 32) * 5 / 9).toFixed(2); |
| 23 | +let fMatch; |
| 24 | +while ((fMatch = fahrenheitRegex.exec(current.text)) !== null) { |
| 25 | + const fahrenheit = parseFloat(fMatch); |
| 26 | + const celsius = fahrenheitToCelsius(fahrenheit); |
| 27 | + conversions.push(`${formatNumber(fahrenheit)}°F is ${formatNumber(celsius)} degrees in sane units (Celsius).`); |
| 28 | +}a |
| 29 | + |
19 | 30 | const conversionMessage = conversions.join('\n'); |
20 | 31 |
|
21 | 32 | new x_snc_slackerbot.Slacker().send_chat(current, conversionMessage); |
0 commit comments