Weather converter °C/°F conversion

I wanted to share a semi-useful script, primarily if you work between the states and other countries.

Here I wanted to create a simple script that will run both in the CLI/UI. You enter weather in formats like 30c or 90f, which will convert it the other way.

Also, from the UI, it's real-time. So you will get the result while typing.

The output can improve (especially the error state). I might do it later.

// Menu: Weather Degree Converter
// Author: Nadeem Khedr
// Twitter: @nadeemkhedr
let convertToF = (c) => {
return (c * 9) / 5 + 32
}
const convertToC = (f) => {
return ((f - 32) * 5) / 9
}
const getDegreeWithType = (input) => {
const matchRegex = /(\d+)(f|c)/i
const result = matchRegex.exec(input)
if (!result) {
return null
}
return {
degree: parseInt(result[1], 10),
type: result[2].toLowerCase(),
}
}
const degreeConverter = (input) => {
const degreeResult = getDegreeWithType(input)
if (!degreeResult) {
return "You need to enter the unit in this format '30c' or '90f'"
}
const { degree, type } = degreeResult
let oDegree = null
let oType = null
if (type === 'c') {
oDegree = convertToF(degree)
oType = 'F'
} else {
oDegree = convertToC(degree)
oType = 'C'
}
return `${oDegree.toFixed(1)}°${oType}`
}
const deg = await arg(
'Enter degress in °C or °F',
(input) =>
`<div class="text-2xl flex justify-center items-center p-5">
${input ? degreeConverter(input) : `Waiting for input`}
</div>`
)
div(degreeConverter(deg))