From 318d6f1713ca30e5cf6c4b608d33bd564eb0dc17 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Mon, 4 Jul 2022 21:13:08 -0700 Subject: [PATCH] Change separator to only accept single characters For multiple characters, a custom word list should be used --- README.md | 2 +- main.ts | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) 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('')); -- 2.47.3