From: Ardis Lu Date: Tue, 5 Jul 2022 04:13:08 +0000 (-0700) Subject: Change separator to only accept single characters X-Git-Tag: v0.1.0~4 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=318d6f1713ca30e5cf6c4b608d33bd564eb0dc17;p=random-word-generator Change separator to only accept single characters For multiple characters, a custom word list should be used --- diff --git a/README.md b/README.md index 6d448fa..1591739 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Similar to [unique-names-generator](https://github.com/andreasonny83/unique-name ## Flags - `--words`: path to a word list file where each word is delimited by a newline. Default value is `./words/xkcd.txt`. -- `--separator`: string to insert in between each word in the output. Default value is `-`. +- `--separator`: a character to insert in between each word in the output. If multiple characters are provided, a random character is picked for each separator. Default value is `-`. - `--length`: the number of words to output. Default value is `3`. ## Included word lists diff --git a/main.ts b/main.ts index 7849966..65320f0 100644 --- a/main.ts +++ b/main.ts @@ -19,6 +19,9 @@ 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 separator argument +const separators: string[] = separator.toString().split(''); + // Validate and process length argument const len = parseInt(length); if (isNaN(len)) { @@ -28,6 +31,10 @@ const rand = new Uint16Array(len); crypto.getRandomValues(rand); const output: string[] = []; -rand.forEach(v => output.push(dictionary[v % dictionary.length])); +rand.forEach(v => { + output.push(dictionary[v % dictionary.length]); + output.push(separators[v % separators.length]); +}); +output.pop(); -console.log(output.join(separator)); +console.log(output.join(''));