@hastehaul/common 1.0.19 → 1.0.21
Sign up to get free protection for your applications and to get access to all the features.
- package/build/index.d.ts +1 -0
- package/build/index.js +2 -0
- package/build/utils/random-codes.d.ts +1 -2
- package/build/utils/random-codes.js +13 -8
- package/package.json +1 -1
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
@@ -113,6 +113,8 @@ __exportStar(require("./utils/enums"), exports);
|
|
113
113
|
__exportStar(require("./utils/read-dir"), exports);
|
114
114
|
//TODO: DOCUMENT
|
115
115
|
__exportStar(require("./utils/schemas"), exports);
|
116
|
+
//TODO: DOCUMENT
|
117
|
+
__exportStar(require("./utils/random-codes"), exports);
|
116
118
|
/**
|
117
119
|
*? Re-exports all the contents from the "base-producers" module.
|
118
120
|
*
|
@@ -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
|
-
declare function generateRandomCodesAsync(characters
|
11
|
+
export declare function generateRandomCodesAsync(characters?: string, numberOfCodes?: number, codeSize?: number, includeHyphens?: boolean): Promise<string>;
|
@@ -8,6 +8,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
9
|
});
|
10
10
|
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.generateRandomCodesAsync = void 0;
|
11
13
|
/**
|
12
14
|
* Generates random codes as an async function using await.
|
13
15
|
*
|
@@ -15,33 +17,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
15
17
|
* @param {number} numberOfCodes - The total number of codes to generate.
|
16
18
|
* @param {number} codeSize - The size of each individual code (number of characters).
|
17
19
|
* @param {boolean} includeHyphens - Whether to include hyphens between codes.
|
18
|
-
* @param {number} chunkSize - The number of codes to generate in each chunk (iteration).
|
19
20
|
*
|
20
21
|
* @returns {Promise<string>} - A Promise that resolves with the generated codes as a string.
|
21
22
|
*/
|
22
|
-
function generateRandomCodesAsync(characters, numberOfCodes, codeSize, includeHyphens
|
23
|
+
function generateRandomCodesAsync(characters, numberOfCodes, codeSize, includeHyphens) {
|
23
24
|
return __awaiter(this, void 0, void 0, function* () {
|
25
|
+
const chars = characters || "ABCDEFGHIJKLMNOPQURSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+=-,./';[]?><:\"\"}{|";
|
26
|
+
const numOfCodes = numberOfCodes || 1;
|
27
|
+
const codeLength = codeSize || 6;
|
24
28
|
let codes = "";
|
25
29
|
let hyphen = includeHyphens ? "-" : "";
|
26
30
|
let chunkStart = 0;
|
27
31
|
let generatedCodes = new Set();
|
28
32
|
function generateChunk() {
|
29
33
|
return __awaiter(this, void 0, void 0, function* () {
|
30
|
-
for (let i = chunkStart; i < chunkStart +
|
34
|
+
for (let i = chunkStart; i < chunkStart + codeLength && i < numOfCodes; i++) {
|
31
35
|
let code = "";
|
32
|
-
for (let j = 0; j <
|
33
|
-
code +=
|
36
|
+
for (let j = 0; j < codeLength; j++) {
|
37
|
+
code += chars.charAt(Math.floor(Math.random() * chars.length));
|
34
38
|
}
|
35
39
|
if (!generatedCodes.has(code)) {
|
36
40
|
generatedCodes.add(code);
|
37
|
-
codes += code + (i ===
|
41
|
+
codes += code + (i === numOfCodes - 1 ? "" : hyphen);
|
38
42
|
}
|
39
43
|
else {
|
40
44
|
i--;
|
41
45
|
}
|
42
46
|
}
|
43
|
-
chunkStart +=
|
44
|
-
if (chunkStart <
|
47
|
+
chunkStart += codeLength;
|
48
|
+
if (chunkStart < numOfCodes && generatedCodes.size < numOfCodes) {
|
45
49
|
yield generateChunk();
|
46
50
|
}
|
47
51
|
});
|
@@ -50,3 +54,4 @@ function generateRandomCodesAsync(characters, numberOfCodes, codeSize, includeHy
|
|
50
54
|
return codes;
|
51
55
|
});
|
52
56
|
}
|
57
|
+
exports.generateRandomCodesAsync = generateRandomCodesAsync;
|