Unverified Commit ee03e7cd authored by John Crepezzi's avatar John Crepezzi Committed by GitHub
Browse files

Merge pull request #180 from seejohnrun/es6_generators

ES6 generators
parents e12805a8 3b6934e3
Loading
Loading
Loading
Loading
+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;
};
+20 −26
Original line number Diff line number Diff line
// Draws inspiration from pwgen and http://tools.arantius.com/password
var PhoneticKeyGenerator = function() {
  // No options
};

// 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();
  }
  return text;
const randOf = (collection) => {
  return () => {
    return collection[Math.floor(Math.random() * collection.length)];
  };
};

PhoneticKeyGenerator.consonants = 'bcdfghjklmnpqrstvwxyz';
PhoneticKeyGenerator.vowels = 'aeiou';
// Helper methods to get an random vowel or consonant
const randVowel = randOf('aeiou');
const randConsonant = randOf('bcdfghjklmnpqrstvwxyz');

// Get an random vowel
PhoneticKeyGenerator.prototype.randVowel = function() {
  return PhoneticKeyGenerator.vowels[
    Math.floor(Math.random() * PhoneticKeyGenerator.vowels.length)
  ];
};
module.exports = class PhoneticKeyGenerator {

// Get an random consonant
PhoneticKeyGenerator.prototype.randConsonant = function() {
  return PhoneticKeyGenerator.consonants[
    Math.floor(Math.random() * PhoneticKeyGenerator.consonants.length)
  ];
};
  // Generate a phonetic key of alternating consonant & vowel
  createKey(keyLength) {
    let text = '';
    const start = Math.round(Math.random());

module.exports = PhoneticKeyGenerator;
    for (let i = 0; i < keyLength; i++) {
      text += (i % 2 == start) ? randConsonant() : randVowel();
    }

    return text;
  }

};
+16 −15
Original line number Diff line number Diff line
var RandomKeyGenerator = function(options) {
  if (!options) {
    options = {};
  }
module.exports = class RandomKeyGenerator {

  // Initialize a new generator with the given keySpace
  constructor(options = {}) {
    this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
};
  }

// Generate a random key
RandomKeyGenerator.prototype.createKey = function(keyLength) {
  // Generate a key of the given length
  createKey(keyLength) {
    var text = '';
  var index;

    for (var i = 0; i < keyLength; i++) {
    index = Math.floor(Math.random() * this.keyspace.length);
      const index = Math.floor(Math.random() * this.keyspace.length);
      text += this.keyspace.charAt(index);
    }

    return text;
};
  }

module.exports = RandomKeyGenerator;
};
+1 −1
Original line number Diff line number Diff line
@@ -46,6 +46,6 @@
  },
  "scripts": {
    "start": "node server.js",
    "test": "mocha"
    "test": "mocha --recursive"
  }
}
+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