From 5a729b635dae0f45617e4e4f3b1fa038f66ee4ac Mon Sep 17 00:00:00 2001 From: jacoobes Date: Wed, 26 Jan 2022 13:02:04 -0600 Subject: [PATCH] added arg preprocessors --- src/handler/utils/preprocessors/args.ts | 23 +++++++++++++++++++++++ src/index.ts | 1 + src/types/handler/handler.ts | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/handler/utils/preprocessors/args.ts diff --git a/src/handler/utils/preprocessors/args.ts b/src/handler/utils/preprocessors/args.ts new file mode 100644 index 0000000..fde1a50 --- /dev/null +++ b/src/handler/utils/preprocessors/args.ts @@ -0,0 +1,23 @@ +import { Err, Ok, Result } from "ts-results"; +import type { possibleOutput } from "../../../types/handler/handler"; + + +export namespace Utils { + /** + * Wrapper type taking `Ok(T)` or `Err(possibleOutput)` e.g `Result = Result + /** + * + * @param {string} arg + * @param {possibleOutput} onFailure + * @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); + } + +} + + diff --git a/src/index.ts b/src/index.ts index a546de8..775865c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { Sern } from "./handler/sern/sern"; + module.exports = Sern; export default Sern; \ No newline at end of file diff --git a/src/types/handler/handler.ts b/src/types/handler/handler.ts index 195e953..bf7d277 100644 --- a/src/types/handler/handler.ts +++ b/src/types/handler/handler.ts @@ -1,7 +1,7 @@ import type { Ok, Result } from 'ts-results'; import type { Awaitable, Client, Message, MessagePayload} from 'discord.js'; import type { MessageOptions } from 'child_process'; -import type Sern from '../..'; +import type { Sern } from '../../handler/sern/sern'; export type Visibility = "private" | "public" export type possibleOutput = string | MessagePayload & MessageOptions