Commit e4e025f6 authored by John Crepezzi's avatar John Crepezzi
Browse files

Convert random generator to es6 and add some specs for it directly

parent e12805a8
Loading
Loading
Loading
Loading
+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"
  }
}
+19 −0
Original line number Diff line number Diff line
/* 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));
    });
  });
});