Phonetic key generator to es6 and add some tests
This commit is contained in:
parent
f161cc33b4
commit
3b6934e348
2 changed files with 49 additions and 28 deletions
|
@ -1,33 +1,27 @@
|
|||
// Draws inspiration from pwgen and http://tools.arantius.com/password
|
||||
var PhoneticKeyGenerator = function() {
|
||||
// No options
|
||||
|
||||
const randOf = (collection) => {
|
||||
return () => {
|
||||
return collection[Math.floor(Math.random() * collection.length)];
|
||||
};
|
||||
};
|
||||
|
||||
// Generate a phonetic key
|
||||
PhoneticKeyGenerator.prototype.createKey = function(keyLength) {
|
||||
var text = '';
|
||||
var start = Math.round(Math.random());
|
||||
for (var i = 0; i < keyLength; i++) {
|
||||
text += (i % 2 == start) ? this.randConsonant() : this.randVowel();
|
||||
// Helper methods to get an random vowel or consonant
|
||||
const randVowel = randOf('aeiou');
|
||||
const randConsonant = randOf('bcdfghjklmnpqrstvwxyz');
|
||||
|
||||
module.exports = class PhoneticKeyGenerator {
|
||||
|
||||
// Generate a phonetic key of alternating consonant & vowel
|
||||
createKey(keyLength) {
|
||||
let text = '';
|
||||
const start = Math.round(Math.random());
|
||||
|
||||
for (let i = 0; i < keyLength; i++) {
|
||||
text += (i % 2 == start) ? randConsonant() : randVowel();
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
return text;
|
||||
|
||||
};
|
||||
|
||||
PhoneticKeyGenerator.consonants = 'bcdfghjklmnpqrstvwxyz';
|
||||
PhoneticKeyGenerator.vowels = 'aeiou';
|
||||
|
||||
// Get an random vowel
|
||||
PhoneticKeyGenerator.prototype.randVowel = function() {
|
||||
return PhoneticKeyGenerator.vowels[
|
||||
Math.floor(Math.random() * PhoneticKeyGenerator.vowels.length)
|
||||
];
|
||||
};
|
||||
|
||||
// Get an random consonant
|
||||
PhoneticKeyGenerator.prototype.randConsonant = function() {
|
||||
return PhoneticKeyGenerator.consonants[
|
||||
Math.floor(Math.random() * PhoneticKeyGenerator.consonants.length)
|
||||
];
|
||||
};
|
||||
|
||||
module.exports = PhoneticKeyGenerator;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue