mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-22 10:43:10 -07:00
Update dictionary.js
This commit is contained in:
parent
26b1617edf
commit
18445795b4
1 changed files with 24 additions and 22 deletions
|
@ -2,31 +2,33 @@ const fs = require('fs');
|
||||||
|
|
||||||
module.exports = class DictionaryGenerator {
|
module.exports = class DictionaryGenerator {
|
||||||
|
|
||||||
constructor(options, readyCallback) {
|
constructor(options, readyCallback) {
|
||||||
// Check options format
|
// 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');
|
if (!options.path) throw Error('No dictionary path specified in options');
|
||||||
|
fs.readFile(options.path, 'utf8', (err, data) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
// Load dictionary
|
this.dictionary = data.split(/[\n\r]+/);
|
||||||
fs.readFile(options.path, 'utf8', (err, data) => {
|
|
||||||
if (err) throw err;
|
|
||||||
|
|
||||||
this.dictionary = data.split(/[\n\r]+/);
|
if (readyCallback) readyCallback();
|
||||||
|
});
|
||||||
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++) {
|
|
||||||
const index = Math.floor(Math.random() * this.dictionary.length);
|
|
||||||
text += this.dictionary[index];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue