parent
f161cc33b4
commit
3b6934e348
2 changed files with 47 additions and 26 deletions
@ -1,33 +1,27 @@ |
|||||||
// Draws inspiration from pwgen and http://tools.arantius.com/password
|
// Draws inspiration from pwgen and http://tools.arantius.com/password
|
||||||
var PhoneticKeyGenerator = function() { |
|
||||||
// No options
|
|
||||||
}; |
|
||||||
|
|
||||||
// Generate a phonetic key
|
const randOf = (collection) => { |
||||||
PhoneticKeyGenerator.prototype.createKey = function(keyLength) { |
return () => { |
||||||
var text = ''; |
return collection[Math.floor(Math.random() * collection.length)]; |
||||||
var start = Math.round(Math.random()); |
}; |
||||||
for (var i = 0; i < keyLength; i++) { |
|
||||||
text += (i % 2 == start) ? this.randConsonant() : this.randVowel(); |
|
||||||
} |
|
||||||
return text; |
|
||||||
}; |
}; |
||||||
|
|
||||||
PhoneticKeyGenerator.consonants = 'bcdfghjklmnpqrstvwxyz'; |
// Helper methods to get an random vowel or consonant
|
||||||
PhoneticKeyGenerator.vowels = 'aeiou'; |
const randVowel = randOf('aeiou'); |
||||||
|
const randConsonant = randOf('bcdfghjklmnpqrstvwxyz'); |
||||||
|
|
||||||
// Get an random vowel
|
module.exports = class PhoneticKeyGenerator { |
||||||
PhoneticKeyGenerator.prototype.randVowel = function() { |
|
||||||
return PhoneticKeyGenerator.vowels[ |
|
||||||
Math.floor(Math.random() * PhoneticKeyGenerator.vowels.length) |
|
||||||
]; |
|
||||||
}; |
|
||||||
|
|
||||||
// Get an random consonant
|
// Generate a phonetic key of alternating consonant & vowel
|
||||||
PhoneticKeyGenerator.prototype.randConsonant = function() { |
createKey(keyLength) { |
||||||
return PhoneticKeyGenerator.consonants[ |
let text = ''; |
||||||
Math.floor(Math.random() * PhoneticKeyGenerator.consonants.length) |
const start = Math.round(Math.random()); |
||||||
]; |
|
||||||
}; |
|
||||||
|
|
||||||
module.exports = PhoneticKeyGenerator; |
for (let i = 0; i < keyLength; i++) { |
||||||
|
text += (i % 2 == start) ? randConsonant() : randVowel(); |
||||||
|
} |
||||||
|
|
||||||
|
return text; |
||||||
|
} |
||||||
|
|
||||||
|
}; |
||||||
|
@ -0,0 +1,27 @@ |
|||||||
|
/* global describe, it */ |
||||||
|
|
||||||
|
const assert = require('assert'); |
||||||
|
|
||||||
|
const Generator = require('../../lib/key_generators/phonetic'); |
||||||
|
|
||||||
|
const vowels = 'aeiou'; |
||||||
|
const consonants = 'bcdfghjklmnpqrstvwxyz'; |
||||||
|
|
||||||
|
describe('RandomKeyGenerator', () => { |
||||||
|
describe('randomKey', () => { |
||||||
|
it('should return a key of the proper length', () => { |
||||||
|
const gen = new Generator(); |
||||||
|
assert.equal(6, gen.createKey(6).length); |
||||||
|
}); |
||||||
|
|
||||||
|
it('should alternate consonants and vowels', () => { |
||||||
|
const gen = new Generator(); |
||||||
|
|
||||||
|
const key = gen.createKey(3); |
||||||
|
|
||||||
|
assert.ok(consonants.includes(key[0])); |
||||||
|
assert.ok(consonants.includes(key[2])); |
||||||
|
assert.ok(vowels.includes(key[1])); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
Loading…
Reference in new issue