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 = {
this.getNum = function(input) { gal: "gal",
let result; l: "L",
lbs: "lbs",
return result; kg: "kg",
}; mi: "mi",
km: "km",
this.getUnit = function(input) {
let result;
return result;
};
this.getReturnUnit = function(initUnit) {
let result;
return result;
}; };
this.spellOutUnit = function(unit) { this.alternateUnits = {
let result; gal: "L",
l: "gal",
return result; lbs: "kg",
kg: "lbs",
mi: "km",
km: "mi",
}; };
this.convert = function(initNum, initUnit) { this.spellOutUnits = {
gal: "gallons",
l: "liters",
lbs: "pounds",
kg: "kilograms",
mi: "miles",
km: "kilometers",
};
this.getNum = function (input) {
const matches = input.match(/[^a-zA-Z]+/g);
if (!matches) return 1;
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) {
const matches = input.match(/[a-zA-Z]+/g);
if (matches.length != 1) return null;
return this.standardUnit[matches[0].toLocaleLowerCase()] || null;
};
this.getReturnUnit = function (initUnit) {
return this.alternateUnits[initUnit.toLocaleLowerCase()];
};
this.spellOutUnit = function (unit) {
return this.spellOutUnits[unit.toLocaleLowerCase()];
};
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");
}
});
}; };