From f202a4dafe78804e59f71ab8a180a351b9e025e0 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Mon, 4 Jul 2022 21:01:55 -0700 Subject: [PATCH] Add validation for words and length arguments --- main.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/main.ts b/main.ts index 53db562..7849966 100644 --- a/main.ts +++ b/main.ts @@ -8,10 +8,23 @@ const { words, separator, length } = parse(Deno.args, { } }); -const dictionary: string[] = []; -await Deno.readTextFile(words).then(f => dictionary.push(...f.split(/\r?\n/))); +function abort(error: Error) { + console.error(error); + Deno.exit(1); +} -const rand = new Uint16Array(parseInt(length)); +// Validate and process words argument +const dictionary: string[] = []; +await Deno.readTextFile(words) + .then(f => dictionary.push(...f.split(/\r?\n/))) + .catch(abort); // Will throw if an invalid file path is given + +// Validate and process length argument +const len = parseInt(length); +if (isNaN(len)) { + abort(new TypeError(`${length} is not a number.`)); +} +const rand = new Uint16Array(len); crypto.getRandomValues(rand); const output: string[] = []; -- 2.47.3