@postxl/utils 0.1.28 → 0.1.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/random.d.ts +7 -1
- package/dist/random.d.ts.map +1 -1
- package/dist/random.js +45 -6
- package/dist/random.js.map +1 -1
- package/package.json +1 -1
package/dist/random.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random integer between 0 (inclusive) and max (exclusive).
|
|
3
|
+
*
|
|
4
|
+
* Isomorphic implementation for Node.js, Deno, Bun, and browser environments.
|
|
5
|
+
*/
|
|
6
|
+
export declare function randomInt(max: number): number;
|
|
1
7
|
/**
|
|
2
8
|
* Returns a randomly generated alphanumeric string.
|
|
3
9
|
*/
|
|
4
|
-
export declare
|
|
10
|
+
export declare function generateRandomAlphanumericString(length: number): string;
|
|
5
11
|
//# sourceMappingURL=random.d.ts.map
|
package/dist/random.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAwB7C;AAmBD;;GAEG;AACH,wBAAgB,gCAAgC,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAMvE"}
|
package/dist/random.js
CHANGED
|
@@ -1,17 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
3
|
+
exports.randomInt = randomInt;
|
|
4
|
+
exports.generateRandomAlphanumericString = generateRandomAlphanumericString;
|
|
5
|
+
/**
|
|
6
|
+
* Returns a random integer between 0 (inclusive) and max (exclusive).
|
|
7
|
+
*
|
|
8
|
+
* Isomorphic implementation for Node.js, Deno, Bun, and browser environments.
|
|
9
|
+
*/
|
|
10
|
+
function randomInt(max) {
|
|
11
|
+
// Browser environment
|
|
12
|
+
// prettier-ignore
|
|
13
|
+
if (typeof globalThis.window !== 'undefined') { // NOSONAR
|
|
14
|
+
return browserRandomInt(max);
|
|
15
|
+
}
|
|
16
|
+
// Try Node.js/Bun crypto.randomInt first
|
|
17
|
+
try {
|
|
18
|
+
const req = eval('typeof require !== "undefined" ? require : undefined');
|
|
19
|
+
if (req) {
|
|
20
|
+
const { randomInt } = req('node:crypto'); // NOSONAR
|
|
21
|
+
return randomInt(max);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// Fall through to Web Crypto API
|
|
26
|
+
}
|
|
27
|
+
// Deno or other environments with Web Crypto API
|
|
28
|
+
if (typeof crypto !== 'undefined') {
|
|
29
|
+
return browserRandomInt(max);
|
|
30
|
+
}
|
|
31
|
+
throw new Error('No suitable random number generator available');
|
|
32
|
+
}
|
|
33
|
+
function browserRandomInt(max) {
|
|
34
|
+
// Browser - use rejection sampling to avoid modulo bias
|
|
35
|
+
const randomMax = 0x100000000; // 2^32
|
|
36
|
+
const range = randomMax - (randomMax % max);
|
|
37
|
+
let value;
|
|
38
|
+
do {
|
|
39
|
+
const array = new Uint32Array(1);
|
|
40
|
+
crypto.getRandomValues(array);
|
|
41
|
+
value = array[0];
|
|
42
|
+
} while (value >= range);
|
|
43
|
+
return value % max;
|
|
44
|
+
}
|
|
5
45
|
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
6
46
|
/**
|
|
7
47
|
* Returns a randomly generated alphanumeric string.
|
|
8
48
|
*/
|
|
9
|
-
|
|
49
|
+
function generateRandomAlphanumericString(length) {
|
|
10
50
|
let result = '';
|
|
11
51
|
for (let i = 0; i < length; i++) {
|
|
12
|
-
result += CHARS[
|
|
52
|
+
result += CHARS[randomInt(CHARS.length)];
|
|
13
53
|
}
|
|
14
54
|
return result;
|
|
15
|
-
}
|
|
16
|
-
exports.generateRandomAlphanumericString = generateRandomAlphanumericString;
|
|
55
|
+
}
|
|
17
56
|
//# sourceMappingURL=random.js.map
|
package/dist/random.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":";;AAKA,8BAwBC;AAsBD,4EAMC;AAzDD;;;;GAIG;AACH,SAAgB,SAAS,CAAC,GAAW;IACnC,sBAAsB;IACtB,kBAAkB;IAClB,IAAI,OAAO,UAAU,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC,CAAC,UAAU;QACxD,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAA;IAC9B,CAAC;IAED,yCAAyC;IACzC,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,CAAC,sDAAsD,CAA4B,CAAA;QACnG,IAAI,GAAG,EAAE,CAAC;YACR,MAAM,EAAE,SAAS,EAAE,GAAG,GAAG,CAAC,aAAa,CAAiC,CAAA,CAAC,UAAU;YACnF,OAAO,SAAS,CAAC,GAAG,CAAC,CAAA;QACvB,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,iCAAiC;IACnC,CAAC;IAED,iDAAiD;IACjD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAA;IAC9B,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAA;AAClE,CAAC;AAED,SAAS,gBAAgB,CAAC,GAAW;IACnC,wDAAwD;IACxD,MAAM,SAAS,GAAG,WAAW,CAAA,CAAC,OAAO;IACrC,MAAM,KAAK,GAAG,SAAS,GAAG,CAAC,SAAS,GAAG,GAAG,CAAC,CAAA;IAE3C,IAAI,KAAa,CAAA;IACjB,GAAG,C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