mirror of
https://github.com/SunRed/haste-server.git
synced 2025-09-04 04:50:14 +02:00
Rewrote key generator tests, fixed vowels
-Tests were not brought up to date with some updates that were done to key generator modules. -Apparently original creator had no idea that 'y' is a vowel; fixed this issue and reflected changes in actual files.
This commit is contained in:
parent
d2c5641886
commit
7808ccf009
7 changed files with 55 additions and 65 deletions
|
@ -3,21 +3,22 @@ const fs = require('fs');
|
|||
|
||||
module.exports = class DictionaryGenerator {
|
||||
|
||||
constructor({ path } = {}, readyCallback){
|
||||
constructor({ path } = {}){
|
||||
//check for dictionary path
|
||||
if (!path){
|
||||
winston.error('No dictionary path specified in options');
|
||||
process.exit(1);
|
||||
let error = 'No dictionary path specified in options';
|
||||
winston.error(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
//load dictionary
|
||||
if (!fs.existsSync(path)){
|
||||
winston.error(`Dictionary file "${path}" doesn't exist`);
|
||||
process.exit(1);
|
||||
let error = `Dictionary file "${path}" doesn't exist`;
|
||||
winston.error(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
this.dictionary = fs.readFileSync(path).toString().split(/\s+/gm);
|
||||
if (readyCallback) readyCallback();
|
||||
}
|
||||
|
||||
// Generates a dictionary-based key, of keyLength words
|
||||
|
|
|
@ -1,18 +1,20 @@
|
|||
//inspiration from pwgen and https://tools.arantius.com/password
|
||||
|
||||
//helper function to get a random consonant / vowel
|
||||
//helper function to get a random array element
|
||||
function randArray(collection){
|
||||
return collection[ Math.floor(Math.random() * collection.length) ];
|
||||
}
|
||||
|
||||
const vovels = 'aeiou';
|
||||
const consonants = 'bcdfghjklmnpqrstvwxyz';
|
||||
const vovels = 'aeiouy';
|
||||
const consonants = 'bcdfghjklmnpqrstvwxz';
|
||||
|
||||
module.exports = class PhoneticKeyGenerator {
|
||||
//generate a phonetic key consisting of random consonant & vowel
|
||||
createKey(keyLength){
|
||||
let text = '';
|
||||
const start = Math.floor(Math.random() * 2);
|
||||
//start == 0 - starts with consonant
|
||||
//start == 1 - starts with vovel
|
||||
|
||||
for (let i = 0; i < keyLength; i++){
|
||||
text += randArray(i % 2 == start ? consonants : vovels);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue