]> git.ardis.lu - random-word-generator/commitdiff
Change separator to only accept single characters
authorArdis Lu <ardisglu@gmail.com>
Tue, 5 Jul 2022 04:13:08 +0000 (21:13 -0700)
committerArdis Lu <ardisglu@gmail.com>
Tue, 5 Jul 2022 04:13:08 +0000 (21:13 -0700)
For multiple characters, a custom word list should be used

README.md
main.ts

index 6d448fadb339ad82b1a1e9243db66cb47cc148f5..1591739de6a0eec410094d3503b2b0717443f07a 100644 (file)
--- 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 78499660677cf24ed69226914bfd65a362f2cecc..65320f0a2be9c8c15616de59652bde5f40223b0d 100644 (file)
--- 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(''));
This page took 0.043422 seconds and 4 git commands to generate.