1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 17:50:19 +01:00

Rewrote phonetic key generator

This commit is contained in:
zneix 2020-08-29 00:39:07 +02:00
parent 15b8899629
commit 2e2b69c43c

View file

@ -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) => { //helper function to get a random consonant / vowel
return () => { function randArray(collection){
return collection[ Math.floor(Math.random() * collection.length) ]; return collection[ Math.floor(Math.random() * collection.length) ];
}; }
};
// Helper methods to get an random vowel or consonant const vovels = 'aeiou';
const randVowel = randOf('aeiou'); const consonants = 'bcdfghjklmnpqrstvwxyz';
const randConsonant = randOf('bcdfghjklmnpqrstvwxyz');
module.exports = class PhoneticKeyGenerator { module.exports = class PhoneticKeyGenerator {
//generate a phonetic key consisting of random consonant & vowel
// Generate a phonetic key of alternating consonant & vowel
createKey(keyLength){ createKey(keyLength){
let text = ''; let text = '';
const start = Math.floor(Math.random() * 2); const start = Math.floor(Math.random() * 2);
for (let i = 0; i < keyLength; i++){ for (let i = 0; i < keyLength; i++){
text += (i % 2 == start) ? randConsonant() : randVowel(); text += randArray(i % 2 == start ? consonants : vovels);
} }
return text; return text;
} }