mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-27 03:10:18 +01:00
Rewrote phonetic key generator
This commit is contained in:
parent
15b8899629
commit
2e2b69c43c
1 changed files with 9 additions and 13 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue