Update lib/key_generators/phonetic.js

This commit is contained in:
colin 2023-12-27 15:32:21 +00:00
parent 7b5b55da5e
commit 6abc863848
1 changed files with 15 additions and 3 deletions

View File

@ -1,13 +1,25 @@
const vowels = 'aeiou';
const consonants = 'bcdfghjklmnpqrstvwxyz';
const randOf = (collection) => {
return () => collection[Math.floor(Math.random() * collection.length)];
};
const randVowel = randOf(vowels);
const randConsonant = randOf(consonants);
module.exports = class PhoneticKeyGenerator {
createKey(keyLength) {
let text = '';
let isConsonant = Math.random() < 0.5;
let chooseConsonant = Math.random() < 0.5; // Randomly start with consonant or vowel
for (let i = 0; i < keyLength; i++) {
text += isConsonant ? randConsonant() : randVowel();
isConsonant = !isConsonant; // Toggle between consonant and vowel
text += chooseConsonant ? randConsonant() : randVowel();
chooseConsonant = !chooseConsonant; // Alternate between consonant and vowel
}
return text;
}
};