@alwatr/random 5.1.0
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/CHANGELOG.md +43 -0
- package/LICENSE +661 -0
- package/README.md +212 -0
- package/dist/main.cjs +132 -0
- package/dist/main.cjs.map +7 -0
- package/dist/main.d.ts +157 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.mjs +97 -0
- package/dist/main.mjs.map +7 -0
- package/package.json +94 -0
- package/src/main.test.js +377 -0
package/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# @alwatr/random
|
|
2
|
+
|
|
3
|
+
A lightweight, high-performance utility library for generating random numbers, strings, UUIDs and more.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
yarn add @alwatr/random
|
|
9
|
+
# or
|
|
10
|
+
npm install @alwatr/random
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- Cross-platform (Node.js and browsers)
|
|
16
|
+
- TypeScript support with type safety
|
|
17
|
+
- Zero dependencies (except for internal package tracer)
|
|
18
|
+
- Uses cryptographic random when available
|
|
19
|
+
- Comprehensive set of random utilities
|
|
20
|
+
- Supports tree-shaking with individually exported functions
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import {
|
|
26
|
+
randNumber,
|
|
27
|
+
randInteger,
|
|
28
|
+
randFloat,
|
|
29
|
+
randString,
|
|
30
|
+
randStep,
|
|
31
|
+
randShuffle,
|
|
32
|
+
randPick,
|
|
33
|
+
randUuid,
|
|
34
|
+
randBoolean,
|
|
35
|
+
randColor,
|
|
36
|
+
randArray,
|
|
37
|
+
bytesToHex
|
|
38
|
+
} from '@alwatr/random';
|
|
39
|
+
|
|
40
|
+
// Get random number between 0 and 1
|
|
41
|
+
console.log(randNumber()); // 0.7124123
|
|
42
|
+
|
|
43
|
+
// Get random integer in range
|
|
44
|
+
console.log(randInteger(1, 10)); // 7
|
|
45
|
+
|
|
46
|
+
// Get random float in range
|
|
47
|
+
console.log(randFloat(1, 10)); // 7.214124
|
|
48
|
+
|
|
49
|
+
// Get random string with fixed length
|
|
50
|
+
console.log(randString(6)); // "A1b2C3"
|
|
51
|
+
|
|
52
|
+
// Get random string with variable length
|
|
53
|
+
console.log(randString(3, 6)); // "X2fg"
|
|
54
|
+
|
|
55
|
+
// Get random string with custom character set
|
|
56
|
+
console.log(randString(5, undefined, '01')); // "10101"
|
|
57
|
+
|
|
58
|
+
// Get random value with step
|
|
59
|
+
console.log(randStep(0, 10, 2)); // 0, 2, 4, 6, 8, or 10
|
|
60
|
+
|
|
61
|
+
// Shuffle an array
|
|
62
|
+
const array = [1, 2, 3, 4, 5];
|
|
63
|
+
console.log(randShuffle([...array])); // [3, 1, 5, 2, 4]
|
|
64
|
+
|
|
65
|
+
// Pick a random item from array
|
|
66
|
+
console.log(randPick(array)); // 3
|
|
67
|
+
|
|
68
|
+
// Generate UUID
|
|
69
|
+
console.log(randUuid()); // "a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"
|
|
70
|
+
|
|
71
|
+
// Generate random boolean
|
|
72
|
+
console.log(randBoolean()); // true or false (50% chance)
|
|
73
|
+
console.log(randBoolean(0.8)); // true or false (80% chance of true)
|
|
74
|
+
|
|
75
|
+
// Generate random color
|
|
76
|
+
console.log(randColor()); // "#a1b2c3"
|
|
77
|
+
|
|
78
|
+
// Convert bytes to hexadecimal string
|
|
79
|
+
const bytes = new Uint8Array([10, 255, 0, 16]);
|
|
80
|
+
console.log(bytesToHex(bytes)); // "0aff0010"
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## API
|
|
84
|
+
|
|
85
|
+
### `randNumber()`
|
|
86
|
+
|
|
87
|
+
Returns a random float between 0 and 1 (not including 1). This is a direct wrapper around `Math.random()`.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
const value = randNumber(); // 0.7124123
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### `randInteger(min, max)`
|
|
94
|
+
|
|
95
|
+
Returns a random integer between `min` and `max` (inclusive).
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const value = randInteger(1, 10); // Between 1 and 10
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### `randFloat(min, max)`
|
|
102
|
+
|
|
103
|
+
Returns a random float between `min` and `max` (max not included).
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
const value = randFloat(1, 10); // Between 1 and 10 (float)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### `randString(min, max?, chars?)`
|
|
110
|
+
|
|
111
|
+
Generates a random string with characters from the specified character set (defaults to alphanumeric set: `A-Z`, `a-z`, `0-9`).
|
|
112
|
+
|
|
113
|
+
- With one argument: Returns a string of exactly that length
|
|
114
|
+
- With two arguments: Returns a string with random length between min and max (inclusive)
|
|
115
|
+
- With three arguments: Uses the provided character set instead of the default alphanumeric set
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
const fixedLength = randString(6); // "A1b2C3"
|
|
119
|
+
const variableLength = randString(3, 6); // Random length between 3 and 6
|
|
120
|
+
const binaryString = randString(8, undefined, '01'); // "10110010"
|
|
121
|
+
const hexString = randString(6, 6, '0123456789abcdef'); // "a3f28c"
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### `randStep(min, max, step)`
|
|
125
|
+
|
|
126
|
+
Returns a random number between `min` and `max` with the specified step.
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const value = randStep(0, 10, 2); // 0, 2, 4, 6, 8, or 10
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### `randShuffle(array)`
|
|
133
|
+
|
|
134
|
+
Shuffles an array in place using Fisher-Yates algorithm and returns it.
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
const array = [1, 2, 3, 4, 5];
|
|
138
|
+
randShuffle(array); // [3, 1, 5, 2, 4]
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### `randPick(array)`
|
|
142
|
+
|
|
143
|
+
Returns a random element from an array.
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
const array = [1, 2, 3, 4, 5];
|
|
147
|
+
const value = randPick(array); // One random element
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### `randUuid()`
|
|
151
|
+
|
|
152
|
+
Generates a random UUID v4. Uses `crypto.randomUUID()` when available, with a fallback implementation.
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
const id = randUuid(); // "a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### `randBoolean(probability?)`
|
|
159
|
+
|
|
160
|
+
Generates a random boolean with the specified probability of being true (default is 0.5).
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const value = randBoolean(); // true or false (50% chance)
|
|
164
|
+
const mostlyTrue = randBoolean(0.8); // true or false (80% chance of true)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### `randColor()`
|
|
168
|
+
|
|
169
|
+
Generates a random hex color string with 6 hexadecimal digits (representing RGB values).
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
const color = randColor(); // "#a1b2c3"
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### `randArray(array, min?, max?)`
|
|
176
|
+
|
|
177
|
+
Fills a typed array with random integer values within the specified range. The array is modified in place and also returned for chaining.
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
// Fill a Uint8Array with random values (0-255)
|
|
181
|
+
randArray(new Uint8Array(10));
|
|
182
|
+
|
|
183
|
+
// Fill with custom range
|
|
184
|
+
randArray(new Uint16Array(5), 1000, 2000); // Values between 1000-2000
|
|
185
|
+
|
|
186
|
+
// Also works with number arrays
|
|
187
|
+
randArray(new Array<number>(8), -100, 100); // Values between -100 and 100
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### `bytesToHex(bytes)`
|
|
191
|
+
|
|
192
|
+
Converts a Uint8Array or number array into a hexadecimal string representation. Each byte is converted to a two-character hex string (padded with a leading zero if necessary) and concatenated together.
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
const bytes = new Uint8Array([10, 255, 0, 16]);
|
|
196
|
+
const hex = bytesToHex(bytes); // "0aff0010"
|
|
197
|
+
|
|
198
|
+
const array = [171, 205, 3];
|
|
199
|
+
const hex2 = bytesToHex(array); // "abcd03"
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Sponsors
|
|
203
|
+
|
|
204
|
+
The following companies, organizations, and individuals support Nanolib ongoing maintenance and development. Become a Sponsor to get your logo on our README and website.
|
|
205
|
+
|
|
206
|
+
## Contributing
|
|
207
|
+
|
|
208
|
+
Contributions are welcome! Please read our [contribution guidelines](https://github.com/Alwatr/.github/blob/next/CONTRIBUTING.md) before submitting a pull request.
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
This project is licensed under the [AGPL-3.0 License](LICENSE).
|
package/dist/main.cjs
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/* @alwatr/random v5.1.0 */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/main.ts
|
|
22
|
+
var main_exports = {};
|
|
23
|
+
__export(main_exports, {
|
|
24
|
+
bytesToHex: () => bytesToHex,
|
|
25
|
+
randArray: () => randArray,
|
|
26
|
+
randBoolean: () => randBoolean,
|
|
27
|
+
randColor: () => randColor,
|
|
28
|
+
randFloat: () => randFloat,
|
|
29
|
+
randInteger: () => randInteger,
|
|
30
|
+
randNumber: () => randNumber,
|
|
31
|
+
randPick: () => randPick,
|
|
32
|
+
randShuffle: () => randShuffle,
|
|
33
|
+
randStep: () => randStep,
|
|
34
|
+
randString: () => randString,
|
|
35
|
+
randUuid: () => randUuid
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(main_exports);
|
|
38
|
+
var import_global_this = require("@alwatr/global-this");
|
|
39
|
+
var import_package_tracer = require("@alwatr/package-tracer");
|
|
40
|
+
var globalThis = (0, import_global_this.getGlobalThis)();
|
|
41
|
+
__dev_mode__: import_package_tracer.packageTracer.add("@alwatr/random", "5.1.0");
|
|
42
|
+
var hasCrypto = typeof globalThis.crypto !== "undefined";
|
|
43
|
+
function bytesToHex(bytes) {
|
|
44
|
+
let result = "";
|
|
45
|
+
for (const byte of bytes) {
|
|
46
|
+
const hex = byte.toString(16);
|
|
47
|
+
result += hex.length === 1 ? "0" + hex : hex;
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
function randNumber() {
|
|
52
|
+
return Math.random();
|
|
53
|
+
}
|
|
54
|
+
function randFloat(min, max) {
|
|
55
|
+
return Math.random() * (max - min) + min;
|
|
56
|
+
}
|
|
57
|
+
function randInteger(min, max) {
|
|
58
|
+
return Math.floor(randFloat(min, max + 1));
|
|
59
|
+
}
|
|
60
|
+
function randString(minLength, maxLength = minLength, chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
|
|
61
|
+
const length = maxLength === minLength ? minLength : randInteger(minLength, maxLength);
|
|
62
|
+
if (length <= 0) return "";
|
|
63
|
+
const charsLength = chars.length;
|
|
64
|
+
let result = "";
|
|
65
|
+
if (length <= 10) {
|
|
66
|
+
for (let i = 0; i < length; i++) {
|
|
67
|
+
result += chars.charAt(Math.floor(Math.random() * charsLength));
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
const resultArray = new Array(length);
|
|
72
|
+
for (let i = 0; i < length; i++) {
|
|
73
|
+
resultArray[i] = chars.charAt(Math.floor(Math.random() * charsLength));
|
|
74
|
+
}
|
|
75
|
+
return resultArray.join("");
|
|
76
|
+
}
|
|
77
|
+
function randStep(min, max, step) {
|
|
78
|
+
if (step === 0) {
|
|
79
|
+
return min;
|
|
80
|
+
}
|
|
81
|
+
const steps = Math.floor((max - min) / step);
|
|
82
|
+
return min + randInteger(0, steps) * step;
|
|
83
|
+
}
|
|
84
|
+
function randShuffle(array) {
|
|
85
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
86
|
+
const j = randInteger(0, i);
|
|
87
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
88
|
+
}
|
|
89
|
+
return array;
|
|
90
|
+
}
|
|
91
|
+
function randPick(array) {
|
|
92
|
+
if (array.length === 0) throw new Error("Cannot pick from empty array");
|
|
93
|
+
return array[randInteger(0, array.length - 1)];
|
|
94
|
+
}
|
|
95
|
+
function randArray(array, min = 0, max = 255) {
|
|
96
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
97
|
+
array[i] = randInteger(min, max);
|
|
98
|
+
}
|
|
99
|
+
return array;
|
|
100
|
+
}
|
|
101
|
+
function randUuid() {
|
|
102
|
+
if (hasCrypto && globalThis.crypto?.randomUUID) {
|
|
103
|
+
return globalThis.crypto.randomUUID();
|
|
104
|
+
}
|
|
105
|
+
const bytes = randArray(new Uint8Array(16));
|
|
106
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
107
|
+
bytes[8] = bytes[8] & 191 | 128;
|
|
108
|
+
return `${bytesToHex(bytes.subarray(0, 4))}-${bytesToHex(bytes.subarray(4, 6))}-${bytesToHex(bytes.subarray(6, 8))}-${bytesToHex(bytes.subarray(8, 10))}-${bytesToHex(bytes.subarray(10, 16))}`;
|
|
109
|
+
}
|
|
110
|
+
function randBoolean(probability = 0.5) {
|
|
111
|
+
return Math.random() < probability;
|
|
112
|
+
}
|
|
113
|
+
function randColor() {
|
|
114
|
+
const bytes = randArray(new Array(3));
|
|
115
|
+
return `#${bytesToHex(bytes)}`;
|
|
116
|
+
}
|
|
117
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
118
|
+
0 && (module.exports = {
|
|
119
|
+
bytesToHex,
|
|
120
|
+
randArray,
|
|
121
|
+
randBoolean,
|
|
122
|
+
randColor,
|
|
123
|
+
randFloat,
|
|
124
|
+
randInteger,
|
|
125
|
+
randNumber,
|
|
126
|
+
randPick,
|
|
127
|
+
randShuffle,
|
|
128
|
+
randStep,
|
|
129
|
+
randString,
|
|
130
|
+
randUuid
|
|
131
|
+
});
|
|
132
|
+
//# sourceMappingURL=main.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/main.ts"],
|
|
4
|
+
"sourcesContent": ["import {getGlobalThis} from '@alwatr/global-this';\nimport {packageTracer} from '@alwatr/package-tracer';\n\nconst globalThis = getGlobalThis();\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n// Use the native crypto module when available for better randomness\nconst hasCrypto = typeof globalThis.crypto !== 'undefined';\n\n/**\n * Converts a Uint8Array or number array into a hexadecimal string representation.\n * Each byte is converted to a two-character hex string (padded with a leading zero if necessary)\n * and concatenated together to form a single string.\n *\n * @param bytes - The array of bytes to convert to hexadecimal\n * @returns A hexadecimal string representation of the input bytes\n *\n * @example\n * ```ts\n * // Using with Uint8Array\n * const bytes = new Uint8Array([10, 255, 0, 16]);\n * bytesToHex(bytes); // Returns \"0aff0010\"\n *\n * // Using with number array\n * const array = [171, 205, 3];\n * bytesToHex(array); // Returns \"abcd03\"\n * ```\n */\nexport function bytesToHex(bytes: number[] | Uint8Array): string {\n let result = '';\n for (const byte of bytes) {\n const hex = byte.toString(16);\n result += hex.length === 1 ? '0' + hex : hex;\n }\n return result;\n}\n\n/**\n * Returns a float random number between 0 and 1 (1 not included).\n *\n * Example:\n *\n * ```js\n * console.log(randNumber()); // 0.7124123\n * ```\n */\nexport function randNumber(): number {\n return Math.random();\n}\n\n/**\n * Generate a random float number between min and max (max not included).\n *\n * Example:\n *\n * ```js\n * console.log(randFloat(1, 10)); // somewhere between 1 and 10 (as float)\n * ```\n */\nexport function randFloat(min: number, max: number): number {\n return Math.random() * (max - min) + min;\n}\n\n/**\n * Generate a random integer number between min and max (max included).\n *\n * Example:\n *\n * ```js\n * console.log(randInteger(1, 10)); // somewhere between 1 and 10\n * ```\n */\nexport function randInteger(min: number, max: number): number {\n // Use Math.floor and add 1 to max for better distribution\n return Math.floor(randFloat(min, max + 1));\n}\n\n/**\n * Generate a random string with specified length.\n * The string will contain only characters from the characters list.\n * The length of the string will be between min and max (max included).\n * If max not specified, the length will be set to min.\n *\n * Example:\n *\n *```js\n * console.log(randString(6)); // something like 'Aab1V2'\n * console.log(randString(3, 6)); // random length between 3 and 6\n * console.log(randString(5, undefined, '01')); // binary string like '10101'\n * ```\n */\nexport function randString(\n minLength: number,\n maxLength: number = minLength,\n chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',\n): string {\n const length = maxLength === minLength ? minLength : randInteger(minLength, maxLength);\n if (length <= 0) return '';\n\n const charsLength = chars.length;\n\n let result = '';\n\n // Small optimization for short strings\n if (length <= 10) {\n for (let i = 0; i < length; i++) {\n result += chars.charAt(Math.floor(Math.random() * charsLength));\n }\n return result;\n }\n // else\n // For longer strings, use array join for better performance\n const resultArray = new Array(length);\n for (let i = 0; i < length; i++) {\n resultArray[i] = chars.charAt(Math.floor(Math.random() * charsLength));\n }\n return resultArray.join('');\n}\n\n/**\n * Generate a random integer between min and max with a step.\n *\n * Example:\n *\n * ```js\n * console.log(randStep(6, 10, 2)); // 6 or 8 or 10\n * ```\n */\nexport function randStep(min: number, max: number, step: number): number {\n if (step === 0) {\n return min; // Return min when step is 0 to avoid division by zero\n }\n const steps = Math.floor((max - min) / step);\n return min + randInteger(0, steps) * step;\n}\n\n/**\n * Shuffle an array in place using Fisher-Yates shuffle algorithm and return it.\n *\n * Example:\n *\n * ```js\n * const array = [1, 2, 3, 4, 5];\n * randShuffle(array);\n * console.log(array); // [2, 4, 3, 1, 5] (randomized)\n * ```\n */\nexport function randShuffle<T>(array: T[]): T[] {\n for (let i = array.length - 1; i > 0; i--) {\n const j = randInteger(0, i);\n [array[i], array[j]] = [array[j], array[i]];\n }\n return array;\n}\n\n/**\n * Choose a random item from an array.\n * Throws an error if the array is empty.\n *\n * Example:\n *\n * ```js\n * const array = [1, 2, 3, 4, 5];\n * console.log(randPick(array)); // one random element\n * ```\n */\nexport function randPick<T>(array: T[]): T {\n if (array.length === 0) throw new Error('Cannot pick from empty array');\n return array[randInteger(0, array.length - 1)];\n}\n\n/**\n * Fills a typed array with random integer values within the specified range.\n * The array is modified in place and also returned for chaining.\n *\n * @param array - The array to fill with random values (modified in place)\n * @param min - Minimum value (inclusive), defaults to 0\n * @param max - Maximum value (inclusive), defaults to 255\n * @returns The same array that was passed in (for chaining)\n *\n * @example\n * ```ts\n * // Fill a Uint8Array with random values (0-255)\n * randArray(new Uint8Array(10));\n *\n * // Fill with custom range\n * randArray(new Uint16Array(5), 1000, 2000); // Values between 1000-2000\n *\n * // Also works with number arrays\n * randArray(new Array<number>(8), -100, 100); // Values between -100 and 100\n * ```\n */\nexport function randArray<T extends number[] | Uint8Array | Uint16Array | Uint32Array>(array: T, min = 0, max = 255): T {\n for (let i = array.length - 1; i >= 0; i--) {\n array[i] = randInteger(min, max);\n }\n return array;\n}\n\n/**\n * Type alias for a UUID string.\n */\nexport type UUID = `${string}-${string}-${string}-${string}-${string}`;\n\n/**\n * Generate a random UUID (v4).\n *\n * Example:\n *\n * ```js\n * console.log(randUuid()); // \"a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6\"\n * ```\n */\nexport function randUuid(): UUID {\n if (hasCrypto && globalThis.crypto?.randomUUID) {\n return globalThis.crypto.randomUUID() as UUID;\n }\n\n // Fallback implementation\n const bytes = randArray(new Uint8Array(16));\n bytes[6] = (bytes[6] & 0x0f) | 0x40; // version 4\n bytes[8] = (bytes[8] & 0xbf) | 0x80; // variant RFC4122\n\n // prettier-ignore\n return `${\n bytesToHex(bytes.subarray(0, 4))\n }-${\n bytesToHex(bytes.subarray(4, 6))\n }-${\n bytesToHex(bytes.subarray(6, 8))\n }-${\n bytesToHex(bytes.subarray(8, 10))\n }-${\n bytesToHex(bytes.subarray(10, 16))\n }` as UUID;\n}\n\n/**\n * Generate a random boolean with specified probability of being true.\n *\n * Example:\n *\n * ```js\n * console.log(randBoolean()); // 50% chance of true\n * console.log(randBoolean(0.8)); // 80% chance of true\n * ```\n */\nexport function randBoolean(probability = 0.5): boolean {\n return Math.random() < probability;\n}\n\n/**\n * Generate a random hex color string.\n *\n * Example:\n *\n * ```js\n * console.log(randColor()); // \"#a1b2c3\"\n * ```\n */\nexport function randColor(): string {\n const bytes = randArray(new Array<number>(3));\n return `#${bytesToHex(bytes)}`;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA4B;AAC5B,4BAA4B;AAE5B,IAAM,iBAAa,kCAAc;AAEjC,aAAc,qCAAc,IAAI,k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
+
}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a Uint8Array or number array into a hexadecimal string representation.
|
|
3
|
+
* Each byte is converted to a two-character hex string (padded with a leading zero if necessary)
|
|
4
|
+
* and concatenated together to form a single string.
|
|
5
|
+
*
|
|
6
|
+
* @param bytes - The array of bytes to convert to hexadecimal
|
|
7
|
+
* @returns A hexadecimal string representation of the input bytes
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* // Using with Uint8Array
|
|
12
|
+
* const bytes = new Uint8Array([10, 255, 0, 16]);
|
|
13
|
+
* bytesToHex(bytes); // Returns "0aff0010"
|
|
14
|
+
*
|
|
15
|
+
* // Using with number array
|
|
16
|
+
* const array = [171, 205, 3];
|
|
17
|
+
* bytesToHex(array); // Returns "abcd03"
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function bytesToHex(bytes: number[] | Uint8Array): string;
|
|
21
|
+
/**
|
|
22
|
+
* Returns a float random number between 0 and 1 (1 not included).
|
|
23
|
+
*
|
|
24
|
+
* Example:
|
|
25
|
+
*
|
|
26
|
+
* ```js
|
|
27
|
+
* console.log(randNumber()); // 0.7124123
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function randNumber(): number;
|
|
31
|
+
/**
|
|
32
|
+
* Generate a random float number between min and max (max not included).
|
|
33
|
+
*
|
|
34
|
+
* Example:
|
|
35
|
+
*
|
|
36
|
+
* ```js
|
|
37
|
+
* console.log(randFloat(1, 10)); // somewhere between 1 and 10 (as float)
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function randFloat(min: number, max: number): number;
|
|
41
|
+
/**
|
|
42
|
+
* Generate a random integer number between min and max (max included).
|
|
43
|
+
*
|
|
44
|
+
* Example:
|
|
45
|
+
*
|
|
46
|
+
* ```js
|
|
47
|
+
* console.log(randInteger(1, 10)); // somewhere between 1 and 10
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function randInteger(min: number, max: number): number;
|
|
51
|
+
/**
|
|
52
|
+
* Generate a random string with specified length.
|
|
53
|
+
* The string will contain only characters from the characters list.
|
|
54
|
+
* The length of the string will be between min and max (max included).
|
|
55
|
+
* If max not specified, the length will be set to min.
|
|
56
|
+
*
|
|
57
|
+
* Example:
|
|
58
|
+
*
|
|
59
|
+
*```js
|
|
60
|
+
* console.log(randString(6)); // something like 'Aab1V2'
|
|
61
|
+
* console.log(randString(3, 6)); // random length between 3 and 6
|
|
62
|
+
* console.log(randString(5, undefined, '01')); // binary string like '10101'
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function randString(minLength: number, maxLength?: number, chars?: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Generate a random integer between min and max with a step.
|
|
68
|
+
*
|
|
69
|
+
* Example:
|
|
70
|
+
*
|
|
71
|
+
* ```js
|
|
72
|
+
* console.log(randStep(6, 10, 2)); // 6 or 8 or 10
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function randStep(min: number, max: number, step: number): number;
|
|
76
|
+
/**
|
|
77
|
+
* Shuffle an array in place using Fisher-Yates shuffle algorithm and return it.
|
|
78
|
+
*
|
|
79
|
+
* Example:
|
|
80
|
+
*
|
|
81
|
+
* ```js
|
|
82
|
+
* const array = [1, 2, 3, 4, 5];
|
|
83
|
+
* randShuffle(array);
|
|
84
|
+
* console.log(array); // [2, 4, 3, 1, 5] (randomized)
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function randShuffle<T>(array: T[]): T[];
|
|
88
|
+
/**
|
|
89
|
+
* Choose a random item from an array.
|
|
90
|
+
* Throws an error if the array is empty.
|
|
91
|
+
*
|
|
92
|
+
* Example:
|
|
93
|
+
*
|
|
94
|
+
* ```js
|
|
95
|
+
* const array = [1, 2, 3, 4, 5];
|
|
96
|
+
* console.log(randPick(array)); // one random element
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function randPick<T>(array: T[]): T;
|
|
100
|
+
/**
|
|
101
|
+
* Fills a typed array with random integer values within the specified range.
|
|
102
|
+
* The array is modified in place and also returned for chaining.
|
|
103
|
+
*
|
|
104
|
+
* @param array - The array to fill with random values (modified in place)
|
|
105
|
+
* @param min - Minimum value (inclusive), defaults to 0
|
|
106
|
+
* @param max - Maximum value (inclusive), defaults to 255
|
|
107
|
+
* @returns The same array that was passed in (for chaining)
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* // Fill a Uint8Array with random values (0-255)
|
|
112
|
+
* randArray(new Uint8Array(10));
|
|
113
|
+
*
|
|
114
|
+
* // Fill with custom range
|
|
115
|
+
* randArray(new Uint16Array(5), 1000, 2000); // Values between 1000-2000
|
|
116
|
+
*
|
|
117
|
+
* // Also works with number arrays
|
|
118
|
+
* randArray(new Array<number>(8), -100, 100); // Values between -100 and 100
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export declare function randArray<T extends number[] | Uint8Array | Uint16Array | Uint32Array>(array: T, min?: number, max?: number): T;
|
|
122
|
+
/**
|
|
123
|
+
* Type alias for a UUID string.
|
|
124
|
+
*/
|
|
125
|
+
export type UUID = `${string}-${string}-${string}-${string}-${string}`;
|
|
126
|
+
/**
|
|
127
|
+
* Generate a random UUID (v4).
|
|
128
|
+
*
|
|
129
|
+
* Example:
|
|
130
|
+
*
|
|
131
|
+
* ```js
|
|
132
|
+
* console.log(randUuid()); // "a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export declare function randUuid(): UUID;
|
|
136
|
+
/**
|
|
137
|
+
* Generate a random boolean with specified probability of being true.
|
|
138
|
+
*
|
|
139
|
+
* Example:
|
|
140
|
+
*
|
|
141
|
+
* ```js
|
|
142
|
+
* console.log(randBoolean()); // 50% chance of true
|
|
143
|
+
* console.log(randBoolean(0.8)); // 80% chance of true
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export declare function randBoolean(probability?: number): boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Generate a random hex color string.
|
|
149
|
+
*
|
|
150
|
+
* Example:
|
|
151
|
+
*
|
|
152
|
+
* ```js
|
|
153
|
+
* console.log(randColor()); // "#a1b2c3"
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export declare function randColor(): string;
|
|
157
|
+
//# sourceMappingURL=main.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,UAAU,GAAG,MAAM,CAO/D;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAE1D;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAG5D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,UAAU,CACxB,SAAS,EAAE,MAAM,EACjB,SAAS,GAAE,MAAkB,EAC7B,KAAK,SAAmE,GACvE,MAAM,CAsBR;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAMvE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAM9C;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,CAGzC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,UAAU,GAAG,WAAW,GAAG,WAAW,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,SAAI,EAAE,GAAG,SAAM,GAAG,CAAC,CAKtH;AAED;;GAEG;AACH,MAAM,MAAM,IAAI,GAAG,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;AAEvE;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,IAAI,IAAI,CAsB/B;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,WAAW,SAAM,GAAG,OAAO,CAEtD;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAGlC"}
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/* @alwatr/random v5.1.0 */
|
|
2
|
+
|
|
3
|
+
// src/main.ts
|
|
4
|
+
import { getGlobalThis } from "@alwatr/global-this";
|
|
5
|
+
import { packageTracer } from "@alwatr/package-tracer";
|
|
6
|
+
var globalThis = getGlobalThis();
|
|
7
|
+
__dev_mode__: packageTracer.add("@alwatr/random", "5.1.0");
|
|
8
|
+
var hasCrypto = typeof globalThis.crypto !== "undefined";
|
|
9
|
+
function bytesToHex(bytes) {
|
|
10
|
+
let result = "";
|
|
11
|
+
for (const byte of bytes) {
|
|
12
|
+
const hex = byte.toString(16);
|
|
13
|
+
result += hex.length === 1 ? "0" + hex : hex;
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
function randNumber() {
|
|
18
|
+
return Math.random();
|
|
19
|
+
}
|
|
20
|
+
function randFloat(min, max) {
|
|
21
|
+
return Math.random() * (max - min) + min;
|
|
22
|
+
}
|
|
23
|
+
function randInteger(min, max) {
|
|
24
|
+
return Math.floor(randFloat(min, max + 1));
|
|
25
|
+
}
|
|
26
|
+
function randString(minLength, maxLength = minLength, chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
|
|
27
|
+
const length = maxLength === minLength ? minLength : randInteger(minLength, maxLength);
|
|
28
|
+
if (length <= 0) return "";
|
|
29
|
+
const charsLength = chars.length;
|
|
30
|
+
let result = "";
|
|
31
|
+
if (length <= 10) {
|
|
32
|
+
for (let i = 0; i < length; i++) {
|
|
33
|
+
result += chars.charAt(Math.floor(Math.random() * charsLength));
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
const resultArray = new Array(length);
|
|
38
|
+
for (let i = 0; i < length; i++) {
|
|
39
|
+
resultArray[i] = chars.charAt(Math.floor(Math.random() * charsLength));
|
|
40
|
+
}
|
|
41
|
+
return resultArray.join("");
|
|
42
|
+
}
|
|
43
|
+
function randStep(min, max, step) {
|
|
44
|
+
if (step === 0) {
|
|
45
|
+
return min;
|
|
46
|
+
}
|
|
47
|
+
const steps = Math.floor((max - min) / step);
|
|
48
|
+
return min + randInteger(0, steps) * step;
|
|
49
|
+
}
|
|
50
|
+
function randShuffle(array) {
|
|
51
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
52
|
+
const j = randInteger(0, i);
|
|
53
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
54
|
+
}
|
|
55
|
+
return array;
|
|
56
|
+
}
|
|
57
|
+
function randPick(array) {
|
|
58
|
+
if (array.length === 0) throw new Error("Cannot pick from empty array");
|
|
59
|
+
return array[randInteger(0, array.length - 1)];
|
|
60
|
+
}
|
|
61
|
+
function randArray(array, min = 0, max = 255) {
|
|
62
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
63
|
+
array[i] = randInteger(min, max);
|
|
64
|
+
}
|
|
65
|
+
return array;
|
|
66
|
+
}
|
|
67
|
+
function randUuid() {
|
|
68
|
+
if (hasCrypto && globalThis.crypto?.randomUUID) {
|
|
69
|
+
return globalThis.crypto.randomUUID();
|
|
70
|
+
}
|
|
71
|
+
const bytes = randArray(new Uint8Array(16));
|
|
72
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
73
|
+
bytes[8] = bytes[8] & 191 | 128;
|
|
74
|
+
return `${bytesToHex(bytes.subarray(0, 4))}-${bytesToHex(bytes.subarray(4, 6))}-${bytesToHex(bytes.subarray(6, 8))}-${bytesToHex(bytes.subarray(8, 10))}-${bytesToHex(bytes.subarray(10, 16))}`;
|
|
75
|
+
}
|
|
76
|
+
function randBoolean(probability = 0.5) {
|
|
77
|
+
return Math.random() < probability;
|
|
78
|
+
}
|
|
79
|
+
function randColor() {
|
|
80
|
+
const bytes = randArray(new Array(3));
|
|
81
|
+
return `#${bytesToHex(bytes)}`;
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
bytesToHex,
|
|
85
|
+
randArray,
|
|
86
|
+
randBoolean,
|
|
87
|
+
randColor,
|
|
88
|
+
randFloat,
|
|
89
|
+
randInteger,
|
|
90
|
+
randNumber,
|
|
91
|
+
randPick,
|
|
92
|
+
randShuffle,
|
|
93
|
+
randStep,
|
|
94
|
+
randString,
|
|
95
|
+
randUuid
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=main.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/main.ts"],
|
|
4
|
+
"sourcesContent": ["import {getGlobalThis} from '@alwatr/global-this';\nimport {packageTracer} from '@alwatr/package-tracer';\n\nconst globalThis = getGlobalThis();\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n// Use the native crypto module when available for better randomness\nconst hasCrypto = typeof globalThis.crypto !== 'undefined';\n\n/**\n * Converts a Uint8Array or number array into a hexadecimal string representation.\n * Each byte is converted to a two-character hex string (padded with a leading zero if necessary)\n * and concatenated together to form a single string.\n *\n * @param bytes - The array of bytes to convert to hexadecimal\n * @returns A hexadecimal string representation of the input bytes\n *\n * @example\n * ```ts\n * // Using with Uint8Array\n * const bytes = new Uint8Array([10, 255, 0, 16]);\n * bytesToHex(bytes); // Returns \"0aff0010\"\n *\n * // Using with number array\n * const array = [171, 205, 3];\n * bytesToHex(array); // Returns \"abcd03\"\n * ```\n */\nexport function bytesToHex(bytes: number[] | Uint8Array): string {\n let result = '';\n for (const byte of bytes) {\n const hex = byte.toString(16);\n result += hex.length === 1 ? '0' + hex : hex;\n }\n return result;\n}\n\n/**\n * Returns a float random number between 0 and 1 (1 not included).\n *\n * Example:\n *\n * ```js\n * console.log(randNumber()); // 0.7124123\n * ```\n */\nexport function randNumber(): number {\n return Math.random();\n}\n\n/**\n * Generate a random float number between min and max (max not included).\n *\n * Example:\n *\n * ```js\n * console.log(randFloat(1, 10)); // somewhere between 1 and 10 (as float)\n * ```\n */\nexport function randFloat(min: number, max: number): number {\n return Math.random() * (max - min) + min;\n}\n\n/**\n * Generate a random integer number between min and max (max included).\n *\n * Example:\n *\n * ```js\n * console.log(randInteger(1, 10)); // somewhere between 1 and 10\n * ```\n */\nexport function randInteger(min: number, max: number): number {\n // Use Math.floor and add 1 to max for better distribution\n return Math.floor(randFloat(min, max + 1));\n}\n\n/**\n * Generate a random string with specified length.\n * The string will contain only characters from the characters list.\n * The length of the string will be between min and max (max included).\n * If max not specified, the length will be set to min.\n *\n * Example:\n *\n *```js\n * console.log(randString(6)); // something like 'Aab1V2'\n * console.log(randString(3, 6)); // random length between 3 and 6\n * console.log(randString(5, undefined, '01')); // binary string like '10101'\n * ```\n */\nexport function randString(\n minLength: number,\n maxLength: number = minLength,\n chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',\n): string {\n const length = maxLength === minLength ? minLength : randInteger(minLength, maxLength);\n if (length <= 0) return '';\n\n const charsLength = chars.length;\n\n let result = '';\n\n // Small optimization for short strings\n if (length <= 10) {\n for (let i = 0; i < length; i++) {\n result += chars.charAt(Math.floor(Math.random() * charsLength));\n }\n return result;\n }\n // else\n // For longer strings, use array join for better performance\n const resultArray = new Array(length);\n for (let i = 0; i < length; i++) {\n resultArray[i] = chars.charAt(Math.floor(Math.random() * charsLength));\n }\n return resultArray.join('');\n}\n\n/**\n * Generate a random integer between min and max with a step.\n *\n * Example:\n *\n * ```js\n * console.log(randStep(6, 10, 2)); // 6 or 8 or 10\n * ```\n */\nexport function randStep(min: number, max: number, step: number): number {\n if (step === 0) {\n return min; // Return min when step is 0 to avoid division by zero\n }\n const steps = Math.floor((max - min) / step);\n return min + randInteger(0, steps) * step;\n}\n\n/**\n * Shuffle an array in place using Fisher-Yates shuffle algorithm and return it.\n *\n * Example:\n *\n * ```js\n * const array = [1, 2, 3, 4, 5];\n * randShuffle(array);\n * console.log(array); // [2, 4, 3, 1, 5] (randomized)\n * ```\n */\nexport function randShuffle<T>(array: T[]): T[] {\n for (let i = array.length - 1; i > 0; i--) {\n const j = randInteger(0, i);\n [array[i], array[j]] = [array[j], array[i]];\n }\n return array;\n}\n\n/**\n * Choose a random item from an array.\n * Throws an error if the array is empty.\n *\n * Example:\n *\n * ```js\n * const array = [1, 2, 3, 4, 5];\n * console.log(randPick(array)); // one random element\n * ```\n */\nexport function randPick<T>(array: T[]): T {\n if (array.length === 0) throw new Error('Cannot pick from empty array');\n return array[randInteger(0, array.length - 1)];\n}\n\n/**\n * Fills a typed array with random integer values within the specified range.\n * The array is modified in place and also returned for chaining.\n *\n * @param array - The array to fill with random values (modified in place)\n * @param min - Minimum value (inclusive), defaults to 0\n * @param max - Maximum value (inclusive), defaults to 255\n * @returns The same array that was passed in (for chaining)\n *\n * @example\n * ```ts\n * // Fill a Uint8Array with random values (0-255)\n * randArray(new Uint8Array(10));\n *\n * // Fill with custom range\n * randArray(new Uint16Array(5), 1000, 2000); // Values between 1000-2000\n *\n * // Also works with number arrays\n * randArray(new Array<number>(8), -100, 100); // Values between -100 and 100\n * ```\n */\nexport function randArray<T extends number[] | Uint8Array | Uint16Array | Uint32Array>(array: T, min = 0, max = 255): T {\n for (let i = array.length - 1; i >= 0; i--) {\n array[i] = randInteger(min, max);\n }\n return array;\n}\n\n/**\n * Type alias for a UUID string.\n */\nexport type UUID = `${string}-${string}-${string}-${string}-${string}`;\n\n/**\n * Generate a random UUID (v4).\n *\n * Example:\n *\n * ```js\n * console.log(randUuid()); // \"a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6\"\n * ```\n */\nexport function randUuid(): UUID {\n if (hasCrypto && globalThis.crypto?.randomUUID) {\n return globalThis.crypto.randomUUID() as UUID;\n }\n\n // Fallback implementation\n const bytes = randArray(new Uint8Array(16));\n bytes[6] = (bytes[6] & 0x0f) | 0x40; // version 4\n bytes[8] = (bytes[8] & 0xbf) | 0x80; // variant RFC4122\n\n // prettier-ignore\n return `${\n bytesToHex(bytes.subarray(0, 4))\n }-${\n bytesToHex(bytes.subarray(4, 6))\n }-${\n bytesToHex(bytes.subarray(6, 8))\n }-${\n bytesToHex(bytes.subarray(8, 10))\n }-${\n bytesToHex(bytes.subarray(10, 16))\n }` as UUID;\n}\n\n/**\n * Generate a random boolean with specified probability of being true.\n *\n * Example:\n *\n * ```js\n * console.log(randBoolean()); // 50% chance of true\n * console.log(randBoolean(0.8)); // 80% chance of true\n * ```\n */\nexport function randBoolean(probability = 0.5): boolean {\n return Math.random() < probability;\n}\n\n/**\n * Generate a random hex color string.\n *\n * Example:\n *\n * ```js\n * console.log(randColor()); // \"#a1b2c3\"\n * ```\n */\nexport function randColor(): string {\n const bytes = randArray(new Array<number>(3));\n return `#${bytesToHex(bytes)}`;\n}\n"],
|
|
5
|
+
"mappings": ";;;AAAA,SAAQ,qBAAoB;AAC5B,SAAQ,qBAAoB;AAE5B,IAAM,aAAa,cAAc;AAEjC,aAAc,eAAc,IAAI,kBAAkB,OAAmB;AAGrE,IAAM,YAAY,OAAO,WAAW,WAAW;AAqBxC,SAAS,WAAW,OAA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