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.

Open weather-convert in Script Kit

// Menu: Weather Degree Converter
// Author: Nadeem Khedr
// Twitter: @nadeemkhedr
import "@johnlindquist/kit";
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) {
// try to be smart, if the user enters a number
// we default degree to C if degree is < 50 and F if not
const numberOnly = parseInt(input, 10)
if (numberOnly) {
const degreeType = numberOnly <= 50 ? 'c' : 'f'
return {
degree: numberOnly,
type: degreeType,
}
}
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));