haste-server/lib/key_generators/dictionary.js

27 lines
712 B
JavaScript
Raw Normal View History

2017-06-26 18:10:57 +02:00
var fs = require('fs');
var DictionaryGenerator = function(options) {
//Options
if (!options)
return done(Error('No options passed to generator'));
2017-06-26 18:11:19 +02:00
if (!options.path)
return done(Error('No dictionary path specified in options'));
//Load dictionary
2017-06-26 17:37:04 +02:00
fs.readFile(options.path, 'utf8', (err,data) => {
2017-06-26 18:10:57 +02: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 18:09:13 +02:00
for(var i = 0; i < keyLength; i++)
2017-06-26 18:10:57 +02:00
text += this.dictionary[Math.floor(Math.random() * this.dictionary.length)];
return text;
};
module.exports = DictionaryGenerator;