-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
4,605 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
); | ||
} | ||
}; |
Oops, something went wrong.