Skip to content

Commit

Permalink
Update Convert Celsius to Fahrenheit.js
Browse files Browse the repository at this point in the history
combine the conversions
  • Loading branch information
chelming authored Feb 5, 2025
1 parent c8ad48a commit 0a77e97
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions Parsers/Convert Celsius to Fahrenheit.js
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);

0 comments on commit 0a77e97

Please sign in to comment.