-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.js
More file actions
80 lines (70 loc) · 3.01 KB
/
generator.js
File metadata and controls
80 lines (70 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
+----------------------------------------------------------------------+
| NodeJS-Dictionary-Generator |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Nova Upinel Chow <[email protected]> |
+----------------------------------------------------------------------+
*/
const fs = require('fs');
const readline = require('readline-sync'); // Using readline-sync for simplicity
const characterClasses = {
'1': '0123456789',
'2': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'3': 'abcdefghijklmnopqrstuvwxyz',
'4': '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'5': '0123456789abcdefghijklmnopqrstuvwxyz',
'6': '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
'7': 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
};
function getCharacterClasses(choice) {
return characterClasses[choice] || null;
};
function generateCombinations(charClasses, currentCombination, size, file) {
if (size === 0) {
fs.writeSync(file, `${currentCombination}\n`);
return;
}
for (let char of charClasses) {
generateCombinations(charClasses, currentCombination + char, size - 1, file);
}
}
function generateDictionary(filename, charClasses, minSize, maxSize) {
const fileDescriptor = fs.openSync(filename, 'w');
for (let size = minSize; size <= maxSize; size++) {
generateCombinations(charClasses, '', size, fileDescriptor);
}
fs.closeSync(fileDescriptor);
}
function main() {
const filename = readline.question('Please enter the file name: ');
let charClasses;
while (!charClasses) {
const choice = readline.question(
'1) Numbers\n' +
'2) Capital Letters\n' +
'3) Lowercase Letters\n' +
'4) Numbers + Capital Letters\n' +
'5) Numbers + Lowercase Letters\n' +
'6) Numbers + Capital Letters + Lowercase Letters\n' +
'7) Capital Letters + Lowercase Letters\n' +
'Please select the character class by number: '
);
charClasses = getCharacterClasses(choice);
if (!charClasses) {
console.log("Invalid choice, please try again.");
}
}
const minSize = parseInt(readline.question("What is the min size of the word? "), 10);
const maxSize = parseInt(readline.question("What is the max size of the word? "), 10);
generateDictionary(filename, charClasses, minSize, maxSize);
console.log(`Dictionary file '${filename}' has been created.`);
}
main();