Skip to content

Commit

Permalink
check input
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeljavan committed Mar 7, 2019
1 parent 4569410 commit a561704
Show file tree
Hide file tree
Showing 3 changed files with 4,605 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "1.0.0",
"main": "./lib/mathString.js",
"license": "MIT",
"files": [
"libs"
],
"devDependencies": {
"@types/jest": "^23.3.5",
"husky": "^1.3.1",
Expand Down
37 changes: 37 additions & 0 deletions src/checkPhrase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const checkInput = (inp: string) => {
checkParenthesses(inp);
checkDot(inp);
checkOperator(inp, '\\/');
checkOperator(inp, '\\*');
};

const checkParenthesses = (inp: string) => {
const nOfOpenningParenhesses = numberOfChar(inp, '\\(');
const nOfClosingParenthesses = numberOfChar(inp, '\\)');
if (nOfClosingParenthesses !== nOfOpenningParenhesses) {
throw new Error('number of opening and closing parenthesses are not equal');
}
};
const numberOfChar = (inp: string, char: string) => {
const regExString = new RegExp(`${char}`, 'g');
const res = inp.match(regExString);
return res ? res.length : 0;
};
const checkUseMoreThanOne = (inp: string, char: string) => {
const regExString = new RegExp(`${char}{2,}`, 'g');
const res = inp.match(regExString);
return res ? res.length : 0;
};
const checkDot = (inp: string) => {
const res = checkUseMoreThanOne(inp, '\\.');
if (res > 0) {
throw new Error('the dot is used more than one in one place');
}
};
const checkOperator = (inp: string, operator: string) => {
if (checkUseMoreThanOne(inp, operator) > 0) {
throw new Error(
`the ${operator.replace(/[*/]/g, '')} is used more than one in one place`
);
}
};
Loading

0 comments on commit a561704

Please sign in to comment.