Loading lib/key_generators/dictionary.js +29 −21 Original line number Diff line number Diff line var fs = require('fs'); const fs = require('fs'); var DictionaryGenerator = function(options) { //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 DictionaryGenerator.prototype.createKey = function(keyLength) { var text = ''; for(var i = 0; i < keyLength; i++) text += this.dictionary[Math.floor(Math.random() * this.dictionary.length)]; 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; }; } module.exports = DictionaryGenerator; }; test/key_generators/dictionary_spec.js 0 → 100644 +33 −0 Original line number Diff line number Diff line /* 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)); }); }); }); }); Loading
lib/key_generators/dictionary.js +29 −21 Original line number Diff line number Diff line var fs = require('fs'); const fs = require('fs'); var DictionaryGenerator = function(options) { //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 DictionaryGenerator.prototype.createKey = function(keyLength) { var text = ''; for(var i = 0; i < keyLength; i++) text += this.dictionary[Math.floor(Math.random() * this.dictionary.length)]; 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; }; } module.exports = DictionaryGenerator; };
test/key_generators/dictionary_spec.js 0 → 100644 +33 −0 Original line number Diff line number Diff line /* 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)); }); }); }); });