From e4e025f67e048cdf7d3f923aeedf853766b1372c Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 31 Oct 2017 20:40:43 -0400 Subject: [PATCH 1/4] Convert random generator to es6 and add some specs for it directly --- lib/key_generators/random.js | 33 +++++++++++++++--------------- package.json | 2 +- test/key_generators/random_spec.js | 19 +++++++++++++++++ 3 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 test/key_generators/random_spec.js diff --git a/lib/key_generators/random.js b/lib/key_generators/random.js index cd35f3d..767e26b 100644 --- a/lib/key_generators/random.js +++ b/lib/key_generators/random.js @@ -1,19 +1,20 @@ -var RandomKeyGenerator = function(options) { - if (!options) { - options = {}; - } - this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; -}; +module.exports = class RandomKeyGenerator { -// Generate a random key -RandomKeyGenerator.prototype.createKey = function(keyLength) { - var text = ''; - var index; - for (var i = 0; i < keyLength; i++) { - index = Math.floor(Math.random() * this.keyspace.length); - text += this.keyspace.charAt(index); + // Initialize a new generator with the given keySpace + constructor(options = {}) { + this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; } - return text; -}; -module.exports = RandomKeyGenerator; + // Generate a key of the given length + createKey(keyLength) { + var text = ''; + + for (var i = 0; i < keyLength; i++) { + const index = Math.floor(Math.random() * this.keyspace.length); + text += this.keyspace.charAt(index); + } + + return text; + } + +}; diff --git a/package.json b/package.json index df12c47..1e87fa3 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,6 @@ }, "scripts": { "start": "node server.js", - "test": "mocha" + "test": "mocha --recursive" } } diff --git a/test/key_generators/random_spec.js b/test/key_generators/random_spec.js new file mode 100644 index 0000000..a438fcf --- /dev/null +++ b/test/key_generators/random_spec.js @@ -0,0 +1,19 @@ +/* global describe, it */ + +const assert = require('assert'); + +const Generator = require('../../lib/key_generators/random'); + +describe('RandomKeyGenerator', function() { + describe('randomKey', function() { + it('should return a key of the proper length', function() { + var gen = new Generator(); + assert.equal(6, gen.createKey(6).length); + }); + + it('should use a key from the given keyset if given', () => { + var gen = new Generator({keyspace: 'A'}); + assert.equal('AAAAAA', gen.createKey(6)); + }); + }); +}); From 40f1f2588ed06f599ac1d29ad166f25bac10bb6a Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 31 Oct 2017 20:55:51 -0400 Subject: [PATCH 2/4] Update some es6 --- test/key_generators/random_spec.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/key_generators/random_spec.js b/test/key_generators/random_spec.js index a438fcf..537a809 100644 --- a/test/key_generators/random_spec.js +++ b/test/key_generators/random_spec.js @@ -4,15 +4,15 @@ const assert = require('assert'); const Generator = require('../../lib/key_generators/random'); -describe('RandomKeyGenerator', function() { - describe('randomKey', function() { - it('should return a key of the proper length', function() { - var gen = new Generator(); +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 use a key from the given keyset if given', () => { - var gen = new Generator({keyspace: 'A'}); + const gen = new Generator({keyspace: 'A'}); assert.equal('AAAAAA', gen.createKey(6)); }); }); From f161cc33b469007b01fd11a5e84b59d0999888ee Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 31 Oct 2017 20:55:59 -0400 Subject: [PATCH 3/4] Added tests and converted dictionary key generator to es6 --- lib/key_generators/dictionary.js | 50 +++++++++++++++----------- test/key_generators/dictionary_spec.js | 33 +++++++++++++++++ 2 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 test/key_generators/dictionary_spec.js diff --git a/lib/key_generators/dictionary.js b/lib/key_generators/dictionary.js index 900ac29..0bcbc2e 100644 --- a/lib/key_generators/dictionary.js +++ b/lib/key_generators/dictionary.js @@ -1,24 +1,32 @@ -var fs = require('fs'); +const fs = require('fs'); -var DictionaryGenerator = function(options) { - //Options - if (!options) throw Error('No options passed to generator'); - if (!options.path) throw Error('No dictionary path specified in options'); +module.exports = class DictionaryGenerator { + + constructor(options, readyCallback) { + // Check options format + if (!options) throw Error('No options passed to generator'); + if (!options.path) throw Error('No dictionary path specified in options'); + + // Load dictionary + fs.readFile(options.path, 'utf8', (err, data) => { + if (err) throw err; + + this.dictionary = data.split(/[\n\r]+/); + + if (readyCallback) readyCallback(); + }); + } + + // Generates a dictionary-based key, of keyLength words + createKey(keyLength) { + let text = ''; + + for (let i = 0; i < keyLength; i++) { + const index = Math.floor(Math.random() * this.dictionary.length); + text += this.dictionary[index]; + } + + return text; + } - //Load dictionary - fs.readFile(options.path, 'utf8', (err, data) => { - if (err) throw err; - this.dictionary = data.split(/[\n\r]+/); - }); }; - -//Generates a dictionary-based key, of keyLength words -DictionaryGenerator.prototype.createKey = function(keyLength) { - var text = ''; - for(var i = 0; i < keyLength; i++) - text += this.dictionary[Math.floor(Math.random() * this.dictionary.length)]; - - return text; -}; - -module.exports = DictionaryGenerator; diff --git a/test/key_generators/dictionary_spec.js b/test/key_generators/dictionary_spec.js new file mode 100644 index 0000000..72718c7 --- /dev/null +++ b/test/key_generators/dictionary_spec.js @@ -0,0 +1,33 @@ +/* global describe, it */ + +const assert = require('assert'); + +const fs = require('fs'); + +const Generator = require('../../lib/key_generators/dictionary'); + +describe('RandomKeyGenerator', function() { + describe('randomKey', function() { + it('should throw an error if given no options', () => { + assert.throws(() => { + new Generator(); + }, Error); + }); + + it('should throw an error if given no path', () => { + assert.throws(() => { + new Generator({}); + }, Error); + }); + + it('should return a key of the proper number of words from the given dictionary', () => { + const path = '/tmp/haste-server-test-dictionary'; + const words = ['cat']; + fs.writeFileSync(path, words.join('\n')); + + const gen = new Generator({path}, () => { + assert.equal('catcatcat', gen.createKey(3)); + }); + }); + }); +}); From 3b6934e348355e0783e2c1cdc44959af553c2c7a Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 31 Oct 2017 21:10:25 -0400 Subject: [PATCH 4/4] Phonetic key generator to es6 and add some tests --- lib/key_generators/phonetic.js | 50 ++++++++++++---------------- test/key_generators/phonetic_spec.js | 27 +++++++++++++++ 2 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 test/key_generators/phonetic_spec.js diff --git a/lib/key_generators/phonetic.js b/lib/key_generators/phonetic.js index cb13a19..f281f6b 100644 --- a/lib/key_generators/phonetic.js +++ b/lib/key_generators/phonetic.js @@ -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; diff --git a/test/key_generators/phonetic_spec.js b/test/key_generators/phonetic_spec.js new file mode 100644 index 0000000..14ad9e8 --- /dev/null +++ b/test/key_generators/phonetic_spec.js @@ -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])); + }); + }); +});