@hastehaul/common 1.0.20 → 1.0.21

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,8 +5,7 @@
5
5
  * @param {number} numberOfCodes - The total number of codes to generate.
6
6
  * @param {number} codeSize - The size of each individual code (number of characters).
7
7
  * @param {boolean} includeHyphens - Whether to include hyphens between codes.
8
- * @param {number} chunkSize - The number of codes to generate in each chunk (iteration).
9
8
  *
10
9
  * @returns {Promise<string>} - A Promise that resolves with the generated codes as a string.
11
10
  */
12
- export declare function generateRandomCodesAsync(characters: string, numberOfCodes: number, codeSize: number, includeHyphens: boolean, chunkSize: number): Promise<string>;
11
+ export declare function generateRandomCodesAsync(characters?: string, numberOfCodes?: number, codeSize?: number, includeHyphens?: boolean): Promise<string>;
@@ -17,33 +17,35 @@ exports.generateRandomCodesAsync = void 0;
17
17
  * @param {number} numberOfCodes - The total number of codes to generate.
18
18
  * @param {number} codeSize - The size of each individual code (number of characters).
19
19
  * @param {boolean} includeHyphens - Whether to include hyphens between codes.
20
- * @param {number} chunkSize - The number of codes to generate in each chunk (iteration).
21
20
  *
22
21
  * @returns {Promise<string>} - A Promise that resolves with the generated codes as a string.
23
22
  */
24
- function generateRandomCodesAsync(characters, numberOfCodes, codeSize, includeHyphens, chunkSize) {
23
+ function generateRandomCodesAsync(characters, numberOfCodes, codeSize, includeHyphens) {
25
24
  return __awaiter(this, void 0, void 0, function* () {
25
+ const chars = characters || "ABCDEFGHIJKLMNOPQURSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+=-,./';[]?><:\"\"}{|";
26
+ const numOfCodes = numberOfCodes || 1;
27
+ const codeLength = codeSize || 6;
26
28
  let codes = "";
27
29
  let hyphen = includeHyphens ? "-" : "";
28
30
  let chunkStart = 0;
29
31
  let generatedCodes = new Set();
30
32
  function generateChunk() {
31
33
  return __awaiter(this, void 0, void 0, function* () {
32
- for (let i = chunkStart; i < chunkStart + chunkSize && i < numberOfCodes; i++) {
34
+ for (let i = chunkStart; i < chunkStart + codeLength && i < numOfCodes; i++) {
33
35
  let code = "";
34
- for (let j = 0; j < codeSize; j++) {
35
- code += characters.charAt(Math.floor(Math.random() * characters.length));
36
+ for (let j = 0; j < codeLength; j++) {
37
+ code += chars.charAt(Math.floor(Math.random() * chars.length));
36
38
  }
37
39
  if (!generatedCodes.has(code)) {
38
40
  generatedCodes.add(code);
39
- codes += code + (i === numberOfCodes - 1 ? "" : hyphen);
41
+ codes += code + (i === numOfCodes - 1 ? "" : hyphen);
40
42
  }
41
43
  else {
42
44
  i--;
43
45
  }
44
46
  }
45
- chunkStart += chunkSize;
46
- if (chunkStart < numberOfCodes && generatedCodes.size < numberOfCodes) {
47
+ chunkStart += codeLength;
48
+ if (chunkStart < numOfCodes && generatedCodes.size < numOfCodes) {
47
49
  yield generateChunk();
48
50
  }
49
51
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "1.0.20",
3
+ "version": "1.0.21",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",