From: Ardis Lu Date: Tue, 5 Jul 2022 04:01:55 +0000 (-0700) Subject: Add validation for words and length arguments X-Git-Tag: v0.1.0~5 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=f202a4dafe78804e59f71ab8a180a351b9e025e0;p=random-word-generator Add validation for words and length arguments --- 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, { } }); +function abort(error: Error) { + console.error(error); + Deno.exit(1); +} + +// Validate and process words argument const dictionary: string[] = []; -await Deno.readTextFile(words).then(f => dictionary.push(...f.split(/\r?\n/))); +await Deno.readTextFile(words) + .then(f => dictionary.push(...f.split(/\r?\n/))) + .catch(abort); // Will throw if an invalid file path is given -const rand = new Uint16Array(parseInt(length)); +// 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[] = [];