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