1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-25 14:01:21 +00:00
haste-server/lib/key_generators/dictionary.js

25 lines
688 B
JavaScript
Raw Normal View History

2017-06-26 16:10:57 +00:00
var 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');
//Load dictionary
fs.readFile(options.path, 'utf8', (err, data) => {
2017-06-26 16:10:57 +00:00
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 = '';
2017-06-26 16:09:13 +00:00
for(var i = 0; i < keyLength; i++)
2017-06-26 16:10:57 +00:00
text += this.dictionary[Math.floor(Math.random() * this.dictionary.length)];
return text;
};
module.exports = DictionaryGenerator;