From 2e2b69c43c23b0332fb2e58878d54fd51222e981 Mon Sep 17 00:00:00 2001 From: zneix Date: Sat, 29 Aug 2020 00:39:07 +0200 Subject: [PATCH] Rewrote phonetic key generator --- lib/key_generators/phonetic.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/key_generators/phonetic.js b/lib/key_generators/phonetic.js index 5dd6da2..bfc402c 100644 --- a/lib/key_generators/phonetic.js +++ b/lib/key_generators/phonetic.js @@ -1,26 +1,22 @@ -// Draws inspiration from pwgen and http://tools.arantius.com/password +//inspiration from pwgen and https://tools.arantius.com/password -const randOf = (collection) => { - return () => { - return collection[Math.floor(Math.random() * collection.length)]; - }; -}; +//helper function to get a random consonant / vowel +function randArray(collection){ + return collection[ Math.floor(Math.random() * collection.length) ]; +} -// Helper methods to get an random vowel or consonant -const randVowel = randOf('aeiou'); -const randConsonant = randOf('bcdfghjklmnpqrstvwxyz'); +const vovels = 'aeiou'; +const consonants = 'bcdfghjklmnpqrstvwxyz'; module.exports = class PhoneticKeyGenerator { - - // Generate a phonetic key of alternating consonant & vowel + //generate a phonetic key consisting of random consonant & vowel createKey(keyLength){ let text = ''; const start = Math.floor(Math.random() * 2); for (let i = 0; i < keyLength; i++){ - text += (i % 2 == start) ? randConsonant() : randVowel(); + text += randArray(i % 2 == start ? consonants : vovels); } - return text; }