mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-23 21:21:21 +00:00
Convert random generator to es6 and add some specs for it directly
This commit is contained in:
parent
e12805a8aa
commit
e4e025f67e
3 changed files with 37 additions and 17 deletions
|
@ -1,19 +1,20 @@
|
||||||
var RandomKeyGenerator = function(options) {
|
module.exports = class RandomKeyGenerator {
|
||||||
if (!options) {
|
|
||||||
options = {};
|
|
||||||
}
|
|
||||||
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generate a random key
|
// Initialize a new generator with the given keySpace
|
||||||
RandomKeyGenerator.prototype.createKey = function(keyLength) {
|
constructor(options = {}) {
|
||||||
var text = '';
|
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||||
var index;
|
|
||||||
for (var i = 0; i < keyLength; i++) {
|
|
||||||
index = Math.floor(Math.random() * this.keyspace.length);
|
|
||||||
text += this.keyspace.charAt(index);
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
|
@ -46,6 +46,6 @@
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server.js",
|
"start": "node server.js",
|
||||||
"test": "mocha"
|
"test": "mocha --recursive"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
19
test/key_generators/random_spec.js
Normal file
19
test/key_generators/random_spec.js
Normal file
|
@ -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));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue