parseBool preprocessor

This commit is contained in:
jacoobes
2022-01-26 21:38:44 -06:00
parent 5a729b635d
commit 981c6fb447

View File

@@ -11,12 +11,25 @@ export namespace Utils {
*
* @param {string} arg
* @param {possibleOutput} onFailure
* @returns {ArgType<number>} Attempts to use Number.parseInt() on `arg`
* @returns {ArgType<number>} Attempts to use `Number.parseInt()` on `arg`
*/
export function parseInt(arg: string, onFailure: possibleOutput): ArgType<number> {
const val = Number.parseInt(arg);
return val === NaN ? Err(onFailure) : Ok(val);
}
export function parseBool(
arg: string,
onFailure: possibleOutput = `Cannot parse ${arg} as a boolean`,
regexes : {yesRegex: RegExp, noRegex: RegExp} = {yesRegex : /(yes|y|👍)/gi, noRegex : /(no|n|👎)/gi} ): ArgType<boolean> {
if(arg.match(regexes.yesRegex)) {
return Ok(true);
}
if(arg.match(regexes.noRegex)) {
return Ok(false)
}
return Err(onFailure)
}
}