mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
parseBool preprocessor
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user