From 18445795b4f3a4e44c5c00be509df01769b0d159 Mon Sep 17 00:00:00 2001 From: wioniqle-q <69215407+wioniqle-q@users.noreply.github.com> Date: Mon, 15 Nov 2021 14:55:18 +0300 Subject: [PATCH] Update dictionary.js --- lib/key_generators/dictionary.js | 46 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/key_generators/dictionary.js b/lib/key_generators/dictionary.js index 0bcbc2e..db65a1c 100644 --- a/lib/key_generators/dictionary.js +++ b/lib/key_generators/dictionary.js @@ -2,31 +2,33 @@ const fs = require('fs'); module.exports = class DictionaryGenerator { - constructor(options, readyCallback) { - // Check options format - if (!options) throw Error('No options passed to generator'); - if (!options.path) throw Error('No dictionary path specified in options'); + constructor(options, readyCallback) { + // Check options format + if (options) { + if (!options.path) throw Error('No dictionary path specified in options'); + fs.readFile(options.path, 'utf8', (err, data) => { + if (err) throw err; - // Load dictionary - fs.readFile(options.path, 'utf8', (err, data) => { - if (err) throw err; + this.dictionary = data.split(/[\n\r]+/); - this.dictionary = data.split(/[\n\r]+/); - - if (readyCallback) readyCallback(); - }); - } - - // Generates a dictionary-based key, of keyLength words - createKey(keyLength) { - let text = ''; - - for (let i = 0; i < keyLength; i++) { - const index = Math.floor(Math.random() * this.dictionary.length); - text += this.dictionary[index]; + if (readyCallback) readyCallback(); + }); + } else { + throw Error('No options passed to generator'); + } } - return text; - } + // Generates a dictionary-based key, of keyLength words + createKey(keyLength) { + let text = ''; + + let i; + for (i = 0; i < keyLength; i++) { + const index = Math.floor(Math.random() * this.dictionary.length); + text += this.dictionary[index]; + } + + return text; + } };