From 981c6fb44771badeeb4fc05421a03192da739ffd Mon Sep 17 00:00:00 2001 From: jacoobes Date: Wed, 26 Jan 2022 21:38:44 -0600 Subject: [PATCH] parseBool preprocessor --- src/handler/utils/preprocessors/args.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/handler/utils/preprocessors/args.ts b/src/handler/utils/preprocessors/args.ts index fde1a50..9b0eddd 100644 --- a/src/handler/utils/preprocessors/args.ts +++ b/src/handler/utils/preprocessors/args.ts @@ -11,12 +11,25 @@ export namespace Utils { * * @param {string} arg * @param {possibleOutput} onFailure - * @returns {ArgType} Attempts to use Number.parseInt() on `arg` + * @returns {ArgType} Attempts to use `Number.parseInt()` on `arg` */ export function parseInt(arg: string, onFailure: possibleOutput): ArgType { 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 { + if(arg.match(regexes.yesRegex)) { + return Ok(true); + } + if(arg.match(regexes.noRegex)) { + return Ok(false) + } + return Err(onFailure) + } }