-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
216 lines (191 loc) · 7.59 KB
/
index.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* RandomGenerator - A class for generating customizable random strings.
*
* This class provides a flexible and customizable solution for generating random strings
* based on various character sets and user-defined criteria. It incorporates a predefined
* list of character groups, allowing users to create unique combinations for their specific needs.
*
* Usage:
* - Instantiate the RandomGenerator class with an optional custom string.
* - Utilize the `generate` method to generate random strings with specified characteristics.
*
* Important Considerations:
* - The class supports a variety of character groups, including lowercase, uppercase, numeric,
* symbols, and specials. Users can customize string generation by specifying desired character
* groups in the `vocabulary` parameter of the `generate` method.
* - The `generate` method takes two optional parameters: `nChars` (length of the generated string)
* and `vocabulary` (array of character groups to include).
* - Users can easily extend or modify the predefined character sets to suit their requirements.
* - A set of unit tests is provided to ensure the reliability of the class functionality.
*
* Example:
* ```javascript
* const generator = new RandomGenerator();
* const randomString = generator.generate(15, ['lowercase', 'numeric']);
* console.log(randomString); // Output: "a1b2c3d4e5f6g7"
* ```
*
* @class
*/
class RandomGenerator {
/**
A class for generating random strings with customizable options. A predefined list of characters are used to generate a unique combination. Any custom strings will be saved as "customString"
@function
*/
constructor(charLen, vocab, customString) {
// TODO: IMPLEMENT CUSTOM STRING REPEAT OR INCLUSION
this.customString = customString;
this.customCharacterSetAlphaLower = "abcdefghijklmnopqrstuvwxyz";
this.customCharacterSetAlphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
this.customCharacterSetNumeric = "0123456789";
this.customCharacterSetSymbols = "!@#$%^&*()-_+=<>?";
this.customCharacterSetSpecial = "[]{}|;:',./";
this.customCharacterSetPunctuation = ". , ; :";
this.customCharacterSetMath = "=+-*/";
this.selectedArrayHistory = [];
this.vocabularyOptions = [];
this.currentLengthLimit = 13;
this.defaultLength = 10;
this.defaultVocab = ["all"];
if (vocab) this.defaultVocab = vocab;
if (charLen) this.defaultLength = charLen;
}
/**
* The only purpose of this function is to make sure nothing might throw an error
* @function
*/
#handleEdgeCases = () => {
if (this.vocabularyOptions.length == 0) {
this.vocabularyOptions.push(this.customCharacterSetAlphaLower);
this.vocabularyOptions.push(this.customCharacterSetAlphaUpper);
this.vocabularyOptions.push(this.customCharacterSetNumeric);
}
// TODO: ADD MORE EDGE CASES
};
/**
Generates a random integer within a specified range.
Parameters:
- min: Minimum value of the range.
- max: Maximum value of the range.
Returns: Random integer within the specified range.
@function
*/
#getRandomIntBetween = (min, max) => {
return Math.floor(Math.random() * (max - min)) + min;
};
/**
Generates a new random numeric string based on the specified req.
Parameters:
- nChars: Length of the string to be generated.
- maxGroups: Maximum number of groups to be generated.
Returns: Random numeric string with specified characteristics.
@function
*/
#generateUniqueNumber = () => {
let randomBigInt = BigInt(Date.now());
let randomSmallInt = Math.floor(Math.random() * 10);
randomBigInt *= BigInt(randomSmallInt);
return randomBigInt;
};
/**
The max length of the unique number in version 1.0.0 is 13, hence the #increaseLength function is called
@function
*/
#increaseLength = (uniqueNumber, maxGroups) => {
const preNumber1 = BigInt(this.#getRandomIntBetween(10, 20));
const preNumber2 = BigInt(this.#getRandomIntBetween(10, 20));
for (let i = 0; i < maxGroups; i++) {
let temp = BigInt(uniqueNumber * preNumber1) / preNumber2;
uniqueNumber = BigInt(uniqueNumber.toString() + temp.toString());
}
return uniqueNumber.toString();
};
/**
Retrieves a character from the vocabulary based on the provided value.
Parameters:
- value: Numeric value used to determine the character.
Returns: Character from the vocabulary based on the provided value.
@function
*/
#getCharFromString = (value) => {
let arr_index = this.#getRandomIntBetween(0, this.vocabularyOptions.length);
if (
this.selectedArrayHistory[-1] == arr_index ||
this.selectedArrayHistory[-2] == arr_index ||
this.selectedArrayHistory[-3] == arr_index
) {
arr_index = this.#getRandomIntBetween(0, 7);
}
this.selectedArrayHistory.push(arr_index);
let selectedArray = this.vocabularyOptions[arr_index];
let char_index = value % selectedArray.length;
return selectedArray[char_index];
};
/**
Creates the vocabulary based on the provided criteria.
Parameters:
- vocabulary: Array specifying the character groups to include in the vocabulary.
Returns: None
@function
*/
#createVocabulary = (vocabulary) => {
const addCharSet = (name, set) => {
if (vocabulary.includes(name)) {
this.vocabularyOptions.push(set);
}
};
if (vocabulary.includes("all")) {
this.vocabularyOptions.push(this.customCharacterSetAlphaLower);
this.vocabularyOptions.push(this.customCharacterSetAlphaUpper);
this.vocabularyOptions.push(this.customCharacterSetNumeric);
return;
}
addCharSet("lowercase", this.customCharacterSetAlphaLower);
addCharSet("uppercase", this.customCharacterSetAlphaUpper);
addCharSet("numeric", this.customCharacterSetNumeric);
addCharSet("symbols", this.customCharacterSetSymbols);
addCharSet("specials", this.customCharacterSetSpecial);
// TODO:CHECK IF SPACE NECESSARY
};
/**
Generates a truly random string based on the time and random calculations with specified characteristics.
Parameters:
nChars: Length of the string to be generated.
vocabulary: Array specifying the character groups to include in the string.
valid values : ["all", "uppercase", "lowercase", "numeric", "symbols", "specials"]
Returns: Random string with specified characteristics
@function
*/
generate = (nChars = this.defaultLength, vocabulary = this.defaultVocab) => {
const maxGroups = Math.floor(nChars / this.currentLengthLimit) + 1;
this.#createVocabulary(vocabulary);
// To handle edge cases
this.#handleEdgeCases();
let trulyRandomNumber;
while (true) {
trulyRandomNumber = this.#generateUniqueNumber();
if (trulyRandomNumber !== 0n) {
break;
}
}
trulyRandomNumber = this.#increaseLength(trulyRandomNumber, maxGroups);
let trulyRandomString = "";
const findAndAppendChar = (indexValue) => {
let value = this.#getCharFromString(indexValue);
if (value === undefined)
findAndAppendChar(this.#getRandomIntBetween(0, 10));
else {
trulyRandomString += value;
}
};
for (let i = 0; i < trulyRandomNumber.length; i++) {
findAndAppendChar(trulyRandomNumber[i]);
}
return trulyRandomString.substring(trulyRandomString.length - nChars);
};
instance = (nChars = this.defaultLength, vocabulary = this.defaultVocab) => {
const generator = new RandomGenerator(nChars, vocabulary);
return generator.generate;
};
}
module.exports = RandomGenerator;