From f161cc33b469007b01fd11a5e84b59d0999888ee Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 31 Oct 2017 20:55:59 -0400 Subject: [PATCH] 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)); + }); + }); + }); +});