Skip to content

Commit

Permalink
fix: change gatNum to handle '/' and check valid numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandr0SmS committed Oct 10, 2023
1 parent 00cae66 commit 254fa36
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions api-project-metricimpconverter/controllers/convertHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ function ConvertHandler() {
]

this.getNum = function(input) {
let number;
const trimmedinput = input.replace(/\s/g, "");
const regExp = new RegExp(/(\d*\.?\d*)(\/?)(\d+\.?\d*)(\/?)/);
const numMatch = trimmedinput.match(regExp);

if (!numMatch || /\/$/.test(numMatch[0])) {
console.log('invalid number')
const removedSpace = input.replace(/\s/, '');
const wordsRegExp = new RegExp(/[a-z]*/, 'gi');
const removedWords = removedSpace.replace(wordsRegExp, '');
const slashArray = removedWords.split('/');
if (slashArray.length > 2) {
return 0
} else {
const splitNum = numMatch[0].split('/');
if (splitNum.length > 1) {
number = parseFloat(splitNum[0] / splitNum[1]);
console.log(number)
return number
const numRegExp = new RegExp(/^[+-]?(\d*\.)?\d+$/);
const checkNum = slashArray.every(n => numRegExp.test(n))
console.log(checkNum)
if (checkNum) {
if (slashArray.length == 1) {
console.log(parseFloat(slashArray))
return parseFloat(slashArray);
} else {
console.log(parseFloat(slashArray[0]) / parseFloat(slashArray[1]))
return parseFloat(slashArray[0]) / parseFloat(slashArray[1])
}
} else {
number = parseFloat(splitNum.join());
console.log(number)
return number
return 0
}
};
}
};

this.getUnit = function(input) {
Expand Down

0 comments on commit 254fa36

Please sign in to comment.