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() {
this.getNum = function(input) {
let result;
return result;
this.standardUnit = {
gal: "gal",
l: "L",
lbs: "lbs",
kg: "kg",
mi: "mi",
km: "km",
};
this.getUnit = function(input) {
let result;
return result;
this.alternateUnits = {
gal: "L",
l: "gal",
lbs: "kg",
kg: "lbs",
mi: "km",
km: "mi",
};
this.getReturnUnit = function(initUnit) {
let result;
return result;
this.spellOutUnits = {
gal: "gallons",
l: "liters",
lbs: "pounds",
kg: "kilograms",
mi: "miles",
km: "kilometers",
};
this.spellOutUnit = function(unit) {
let result;
return result;
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.convert = function(initNum, initUnit) {
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 lbsToKg = 0.453592;
const miToKm = 1.60934;
let result;
return result;
switch (initUnit.toLocaleLowerCase()) {
case "gal":
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) {
let result;
return result;
this.getString = function (initNum, initUnit, returnNum, returnUnit) {
return `${initNum} ${this.spellOutUnit(
initUnit
)} converts to ${returnNum} ${this.spellOutUnit(returnUnit)}`;
};
}
module.exports = ConvertHandler;

View File

@ -1,10 +1,48 @@
'use strict';
"use strict";
const expect = require('chai').expect;
const ConvertHandler = require('../controllers/convertHandler.js');
const expect = require("chai").expect;
const ConvertHandler = require("../controllers/convertHandler.js");
module.exports = function (app) {
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");
}
});
};