-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Convert Celsius to Fahrenheit.js
combine the conversions
- Loading branch information
Showing
1 changed file
with
18 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,32 @@ | ||
/* | ||
activation_example:30c or 30 degrees celsius | ||
regex:(?:^|\s)(-?\d{1,3}\.?\d{0,2})°?\s?(?:degrees)?\s?c(?:elsius|elcius)?\b | ||
regex:(?:^|\s)(-?\d{1,3}\.?\d{0,2})°?\s?(?:degrees)?\s?(?:c(?:elsius)?|f(?:ahrenheit)?)\b | ||
flags:gmi | ||
*/ | ||
|
||
const regexTest = /(-?\d{1,3}(?:\.\d{1,2})?)°?\s?(?:degrees?)?\s?c(?:elsius)?\b/gi; | ||
const celsiusToFahrenheit = c => ((c * 9 / 5) + 32).toFixed(2); | ||
const formatNumber = num => Number(num).toFixed(2).replace(/\.00$/, ''); | ||
const conversions = []; | ||
|
||
let match; | ||
while ((match = regexTest.exec(current.text)) !== null) { | ||
const celsius = parseFloat(match); | ||
// convert C to F | ||
const celsiusRegex = /(-?\d{1,3}(?:\.\d{1,2})?)°?\s?(?:degrees?)?\s?c(?:elsius)?\b/gi; | ||
const celsiusToFahrenheit = c => ((c * 9 / 5) + 32).toFixed(2); | ||
let cMatch; | ||
while ((cMatch = celsiusRegex.exec(current.text)) !== null) { | ||
const celsius = parseFloat(cMatch); | ||
const fahrenheit = celsiusToFahrenheit(celsius); | ||
conversions.push(`${formatNumber(celsius)}°C is ${formatNumber(fahrenheit)} degrees in sane units (Fahrenheit).`); | ||
conversions.push(`${formatNumber(celsius)}°C is ${formatNumber(fahrenheit)} degrees in freedom units (Fahrenheit).`); | ||
} | ||
|
||
// convert F to C | ||
const fahrenheitRegex = /(-?\d{1,3}(?:\.\d{1,2})?)°?\s?(?:degrees?)?\s?f(?:ahrenheit)?\b/gi; | ||
const fahrenheitToCelsius = f => ((f - 32) * 5 / 9).toFixed(2); | ||
let fMatch; | ||
while ((fMatch = fahrenheitRegex.exec(current.text)) !== null) { | ||
const fahrenheit = parseFloat(fMatch); | ||
const celsius = fahrenheitToCelsius(fahrenheit); | ||
conversions.push(`${formatNumber(fahrenheit)}°F is ${formatNumber(celsius)} degrees in sane units (Celsius).`); | ||
}a | ||
|
||
const conversionMessage = conversions.join('\n'); | ||
|
||
new x_snc_slackerbot.Slacker().send_chat(current, conversionMessage); |