Update dictionary.js

This commit is contained in:
wioniqle-q 2021-11-15 14:55:18 +03:00 committed by GitHub
commit 18445795b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,10 +4,8 @@ module.exports = class DictionaryGenerator {
constructor(options, readyCallback) {
// Check options format
if (!options) throw Error('No options passed to generator');
if (options) {
if (!options.path) throw Error('No dictionary path specified in options');
// Load dictionary
fs.readFile(options.path, 'utf8', (err, data) => {
if (err) throw err;
@ -15,13 +13,17 @@ module.exports = class DictionaryGenerator {
if (readyCallback) readyCallback();
});
} else {
throw Error('No options passed to generator');
}
}
// Generates a dictionary-based key, of keyLength words
createKey(keyLength) {
let text = '';
for (let i = 0; i < keyLength; i++) {
let i;
for (i = 0; i < keyLength; i++) {
const index = Math.floor(Math.random() * this.dictionary.length);
text += this.dictionary[index];
}