2012-01-07 17:35:11 +01:00
|
|
|
// Draws inspiration from pwgen and http://tools.arantius.com/password
|
|
|
|
|
2017-11-01 02:10:25 +01:00
|
|
|
const randOf = (collection) => {
|
2020-08-28 04:39:03 +02:00
|
|
|
return () => {
|
|
|
|
return collection[Math.floor(Math.random() * collection.length)];
|
|
|
|
};
|
2012-01-07 17:35:11 +01:00
|
|
|
};
|
|
|
|
|
2017-11-01 02:10:25 +01:00
|
|
|
// Helper methods to get an random vowel or consonant
|
|
|
|
const randVowel = randOf('aeiou');
|
|
|
|
const randConsonant = randOf('bcdfghjklmnpqrstvwxyz');
|
2012-01-07 17:35:11 +01:00
|
|
|
|
2017-11-01 02:10:25 +01:00
|
|
|
module.exports = class PhoneticKeyGenerator {
|
2012-01-07 17:35:11 +01:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
// Generate a phonetic key of alternating consonant & vowel
|
|
|
|
createKey(keyLength){
|
|
|
|
let text = '';
|
|
|
|
const start = Math.floor(Math.random() * 2);
|
2012-01-07 17:35:11 +01:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
for (let i = 0; i < keyLength; i++){
|
|
|
|
text += (i % 2 == start) ? randConsonant() : randVowel();
|
|
|
|
}
|
2017-11-01 02:10:25 +01:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
return text;
|
|
|
|
}
|
2017-11-01 02:10:25 +01:00
|
|
|
|
|
|
|
};
|