This commit is contained in:
Manish 2023-04-23 20:11:26 +10:00
parent 7d70bdbe5e
commit 5f6b6e10fc
2 changed files with 129 additions and 37 deletions

View File

@ -1,44 +1,98 @@
function ConvertHandler() { function ConvertHandler() {
this.standardUnit = {
gal: "gal",
l: "L",
lbs: "lbs",
kg: "kg",
mi: "mi",
km: "km",
};
this.alternateUnits = {
gal: "L",
l: "gal",
lbs: "kg",
kg: "lbs",
mi: "km",
km: "mi",
};
this.spellOutUnits = {
gal: "gallons",
l: "liters",
lbs: "pounds",
kg: "kilograms",
mi: "miles",
km: "kilometers",
};
this.getNum = function (input) { this.getNum = function (input) {
let result; const matches = input.match(/[^a-zA-Z]+/g);
if (!matches) return 1;
return result; if (matches.length != 1) return null;
const num_parts = matches[0].split("/");
if (num_parts.length == 1) {
const num = Number(num_parts[0]);
return num >= 0 ? num : null;
}
if (num_parts.length == 2) {
const [numerator, denominator] = [
Number(num_parts[0]),
Number(num_parts[1]),
];
if (
Number.isNaN(numerator) ||
Number.isNaN(denominator) ||
denominator == 0
)
return null;
const num = numerator / denominator;
return num >= 0 ? num : null;
}
return null;
}; };
this.getUnit = function (input) { this.getUnit = function (input) {
let result; const matches = input.match(/[a-zA-Z]+/g);
if (matches.length != 1) return null;
return result; return this.standardUnit[matches[0].toLocaleLowerCase()] || null;
}; };
this.getReturnUnit = function (initUnit) { this.getReturnUnit = function (initUnit) {
let result; return this.alternateUnits[initUnit.toLocaleLowerCase()];
return result;
}; };
this.spellOutUnit = function (unit) { this.spellOutUnit = function (unit) {
let result; return this.spellOutUnits[unit.toLocaleLowerCase()];
return result;
}; };
this.convert = function (initNum, initUnit) { this.convert = function (initNum, initUnit) {
const galToL = 3.78541; const galToL = 3.78541;
const lbsToKg = 0.453592; const lbsToKg = 0.453592;
const miToKm = 1.60934; const miToKm = 1.60934;
let result; switch (initUnit.toLocaleLowerCase()) {
case "gal":
return result; return initNum * galToL;
case "l":
return initNum / galToL;
case "lbs":
return initNum * lbsToKg;
case "kg":
return initNum / lbsToKg;
case "mi":
return initNum * miToKm;
case "km":
return initNum / miToKm;
default:
return null;
}
}; };
this.getString = function (initNum, initUnit, returnNum, returnUnit) { this.getString = function (initNum, initUnit, returnNum, returnUnit) {
let result; return `${initNum} ${this.spellOutUnit(
initUnit
return result; )} converts to ${returnNum} ${this.spellOutUnit(returnUnit)}`;
}; };
} }
module.exports = ConvertHandler; module.exports = ConvertHandler;

View File

@ -1,10 +1,48 @@
'use strict'; "use strict";
const expect = require('chai').expect; const expect = require("chai").expect;
const ConvertHandler = require('../controllers/convertHandler.js'); const ConvertHandler = require("../controllers/convertHandler.js");
module.exports = function (app) { module.exports = function (app) {
let convertHandler = new ConvertHandler(); let convertHandler = new ConvertHandler();
app.route("/api/convert").get((req, res) => {
const input = req.query.input;
if (
input &&
convertHandler.getUnit(input) &&
convertHandler.getNum(input)
) {
const initNum = convertHandler.getNum(input);
const initUnit = convertHandler.getUnit(input);
const returnNum = Number(
convertHandler.convert(initNum, initUnit)?.toFixed(5)
);
const returnUnit = convertHandler.getReturnUnit(initUnit);
const string = convertHandler.getString(
initNum,
initUnit,
returnNum,
returnUnit
);
res.json({
initNum: initNum,
initUnit: initUnit,
returnNum: returnNum,
returnUnit: returnUnit,
string: string,
});
} else if (
!convertHandler.getUnit(input) &&
!convertHandler.getNum(input)
) {
console.log(`1: ${input}`);
res.send("invalid number and unit");
} else if (!convertHandler.getUnit(input)) {
console.log(`2: ${input}`);
res.send("invalid unit");
} else if (!convertHandler.getNum(input)) {
console.log(`3: ${input}`);
res.send("invalid number");
}
});
}; };