1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-01 01:30:21 +01:00
haste-server/lib/key_generators/phonetic.js

26 lines
686 B
JavaScript
Raw Normal View History

2020-08-29 00:39:07 +02:00
//inspiration from pwgen and https://tools.arantius.com/password
//helper function to get a random array element
2020-08-29 00:39:07 +02:00
function randArray(collection){
return collection[ Math.floor(Math.random() * collection.length) ];
}
const vovels = 'aeiouy';
const consonants = 'bcdfghjklmnpqrstvwxz';
module.exports = class PhoneticKeyGenerator {
2020-08-29 00:39:07 +02:00
//generate a phonetic key consisting of random consonant & vowel
2020-08-28 04:39:03 +02:00
createKey(keyLength){
let text = '';
const start = Math.floor(Math.random() * 2);
//start == 0 - starts with consonant
//start == 1 - starts with vovel
2020-08-28 04:39:03 +02:00
for (let i = 0; i < keyLength; i++){
2020-08-29 00:39:07 +02:00
text += randArray(i % 2 == start ? consonants : vovels);
2020-08-28 04:39:03 +02:00
}
return text;
}
};