refactor phonetic.js to match test requirements.
This commit is contained in:
parent
4ae1756c35
commit
23c47e49ec
|
@ -1,27 +1,13 @@
|
||||||
// Draws inspiration from pwgen and http://tools.arantius.com/password
|
|
||||||
|
|
||||||
const randOf = (collection) => {
|
|
||||||
return () => {
|
|
||||||
return collection[Math.floor(Math.random() * collection.length)];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// Helper methods to get an random vowel or consonant
|
|
||||||
const randVowel = randOf('aeiou');
|
|
||||||
const randConsonant = randOf('bcdfghjklmnpqrstvwxyz');
|
|
||||||
|
|
||||||
module.exports = class PhoneticKeyGenerator {
|
module.exports = class PhoneticKeyGenerator {
|
||||||
|
|
||||||
// Generate a phonetic key of alternating consonant & vowel
|
|
||||||
createKey(keyLength) {
|
createKey(keyLength) {
|
||||||
let text = '';
|
let text = '';
|
||||||
const start = Math.round(Math.random());
|
let isConsonant = Math.random() < 0.5;
|
||||||
|
|
||||||
for (let i = 0; i < keyLength; i++) {
|
for (let i = 0; i < keyLength; i++) {
|
||||||
text += (i % 2 == start) ? randConsonant() : randVowel();
|
text += isConsonant ? randConsonant() : randVowel();
|
||||||
|
isConsonant = !isConsonant; // Toggle between consonant and vowel
|
||||||
}
|
}
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue