2020-08-29 00:39:07 +02:00
|
|
|
//inspiration from pwgen and https://tools.arantius.com/password
|
2012-01-07 17:35:11 +01:00
|
|
|
|
2020-09-23 01:07:32 +02:00
|
|
|
//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) ];
|
|
|
|
}
|
2012-01-07 17:35:11 +01:00
|
|
|
|
2020-09-23 01:07:32 +02:00
|
|
|
const vovels = 'aeiouy';
|
|
|
|
const consonants = 'bcdfghjklmnpqrstvwxz';
|
2012-01-07 17:35:11 +01:00
|
|
|
|
2017-11-01 02:10:25 +01:00
|
|
|
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);
|
2020-09-23 01:07:32 +02:00
|
|
|
//start == 0 - starts with consonant
|
|
|
|
//start == 1 - starts with vovel
|
2012-01-07 17:35:11 +01:00
|
|
|
|
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;
|
|
|
|
}
|
2017-11-01 02:10:25 +01:00
|
|
|
|
|
|
|
};
|