From 80f0618736cc3af756e85b910d83490b4e312a96 Mon Sep 17 00:00:00 2001 From: Klas af Geijerstam Date: Mon, 26 Jun 2017 18:03:18 +0200 Subject: [PATCH] Updated dictionary.js Now expects a newline separated dictionary, supports both \n and \n\r --- lib/key_generators/dictionary.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/key_generators/dictionary.js b/lib/key_generators/dictionary.js index ad40b9a..69ebc48 100644 --- a/lib/key_generators/dictionary.js +++ b/lib/key_generators/dictionary.js @@ -11,20 +11,18 @@ var DictionaryGenerator = function(options) { //Load dictionary fs.readFile(options.path, 'utf8', (err,data) => { if(err) throw err; - this.dictionary = data.split(','); - - //Remove any non alpha-numeric characters - for(var i = 0; i < this.dictionary.length; i++) - this.dictionary[i] = this.dictionary[i].replace(/\W/g,''); - - }); + this.dictionary = data.split(/[\n\r]+/); + }); }; //Generates a dictionary-based key, of keyLength words DictionaryGenerator.prototype.createKey = function(keyLength) { var text = ''; - for(var i = 0; i < keyLength; i++) - text += this.dictionary[Math.floor(Math.random()*this.dictionary.length)]; + for(var i = 0; i < keyLength; i++) { + var index =Math.floor(Math.random()*this.dictionary.length); + text += this.dictionary[index]; + } + return text; };