@alwatr/random 5.1.31 → 6.0.1
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/main.js +5 -0
- package/dist/main.js.map +10 -0
- package/package.json +41 -44
- package/src/main.ts +262 -0
- package/CHANGELOG.md +0 -210
- package/dist/main.cjs +0 -3
- package/dist/main.cjs.map +0 -7
- package/dist/main.mjs +0 -3
- package/dist/main.mjs.map +0 -7
package/dist/main.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/* 📦 @alwatr/random v6.0.1 */
|
|
2
|
+
import{getGlobalThis as P}from"@alwatr/global-this";var J=P(),Q=(()=>typeof J.crypto<"u")();function B(f){let k="";for(let v of f){let q=v.toString(16);k+=q.length===1?"0"+q:q}return k}function U(){return Math.random()}function R(f,k){return Math.random()*(k-f)+f}function D(f,k){return Math.floor(R(f,k+1))}function V(f,k=f,v="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"){let q=k===f?f:D(f,k);if(q<=0)return"";let K=v.length,M="";if(q<=10){for(let z=0;z<q;z++)M+=v.charAt(Math.floor(Math.random()*K));return M}let N=Array(q);for(let z=0;z<q;z++)N[z]=v.charAt(Math.floor(Math.random()*K));return N.join("")}function W(f,k,v){if(v===0)return f;let q=Math.floor((k-f)/v);return f+D(0,q)*v}function X(f){for(let k=f.length-1;k>0;k--){let v=D(0,k);[f[k],f[v]]=[f[v],f[k]]}return f}function Y(f){if(f.length===0)throw Error("Cannot pick from empty array");return f[D(0,f.length-1)]}function O(f,k=0,v=255){for(let q=f.length-1;q>=0;q--)f[q]=D(k,v);return f}function Z(){if(Q&&J.crypto?.randomUUID)return J.crypto.randomUUID();let f=O(new Uint8Array(16));return f[6]=f[6]&15|64,f[8]=f[8]&191|128,`${B(f.subarray(0,4))}-${B(f.subarray(4,6))}-${B(f.subarray(6,8))}-${B(f.subarray(8,10))}-${B(f.subarray(10,16))}`}function _(f=0.5){return Math.random()<f}function $(){let f=O([,,,]);return`#${B(f)}`}export{Z as randUuid,V as randString,W as randStep,X as randShuffle,Y as randPick,U as randNumber,D as randInteger,R as randFloat,$ as randColor,_ as randBoolean,O as randArray,B as bytesToHex};
|
|
3
|
+
|
|
4
|
+
//# debugId=9652FDFB4D6F485764756E2164756E21
|
|
5
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/main.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import {getGlobalThis} from '@alwatr/global-this';\n\nconst globalThis = getGlobalThis();\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"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";AAAA,wBAAQ,4BAER,IAAM,EAAa,EAAc,EAG3B,GAAa,IAAM,OAAO,EAAW,OAAW,KAAa,EAqB5D,SAAS,CAAU,CAAC,EAAsC,CAC/D,IAAI,EAAS,GACb,QAAW,KAAQ,EAAO,CACxB,IAAM,EAAM,EAAK,SAAS,EAAE,EAC5B,GAAU,EAAI,SAAW,EAAI,IAAM,EAAM,EAE3C,OAAO,EAYF,SAAS,CAAU,EAAW,CACnC,OAAO,KAAK,OAAO,EAYd,SAAS,CAAS,CAAC,EAAa,EAAqB,CAC1D,OAAO,KAAK,OAAO,GAAK,EAAM,GAAO,EAYhC,SAAS,CAAW,CAAC,EAAa,EAAqB,CAE5D,OAAO,KAAK,MAAM,EAAU,EAAK,EAAM,CAAC,CAAC,EAiBpC,SAAS,CAAU,CACxB,EACA,EAAoB,EACpB,EAAQ,iEACA,CACR,IAAM,EAAS,IAAc,EAAY,EAAY,EAAY,EAAW,CAAS,EACrF,GAAI,GAAU,EAAG,MAAO,GAExB,IAAM,EAAc,EAAM,OAEtB,EAAS,GAGb,GAAI,GAAU,GAAI,CAChB,QAAS,EAAI,EAAG,EAAI,EAAQ,IAC1B,GAAU,EAAM,OAAO,KAAK,MAAM,KAAK,OAAO,EAAI,CAAW,CAAC,EAEhE,OAA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AC,KAEhC,EAAW,EAAM,SAAS,GAAI,EAAE,CAAC,IAc9B,SAAS,CAAW,CAAC,EAAc,IAAc,CACtD,OAAO,KAAK,OAAO,EAAI,EAYlB,SAAS,CAAS,EAAW,CAClC,IAAM,EAAQ,EAAU,KAAoB,EAC5C,MAAO,IAAI,EAAW,CAAK",
|
|
8
|
+
"debugId": "9652FDFB4D6F485764756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,32 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/random",
|
|
3
|
+
"version": "6.0.1",
|
|
3
4
|
"description": "A lightweight utility library for generating random numbers, strings, UUIDs and more",
|
|
4
|
-
"
|
|
5
|
+
"license": "MPL-2.0",
|
|
5
6
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"directory": "packages/random",
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/Alwatr/nanolib"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/random#readme",
|
|
6
14
|
"bugs": "https://github.com/Alwatr/nanolib/issues",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/main.d.ts",
|
|
18
|
+
"default": "./dist/main.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"sideEffects": false,
|
|
7
22
|
"dependencies": {
|
|
8
|
-
"@alwatr/global-this": "
|
|
23
|
+
"@alwatr/global-this": "6.0.1"
|
|
9
24
|
},
|
|
10
25
|
"devDependencies": {
|
|
11
|
-
"@alwatr/nano-build": "
|
|
12
|
-
"@alwatr/prettier-config": "
|
|
13
|
-
"@alwatr/tsconfig-base": "
|
|
26
|
+
"@alwatr/nano-build": "7.0.1",
|
|
27
|
+
"@alwatr/prettier-config": "7.0.1",
|
|
28
|
+
"@alwatr/tsconfig-base": "8.0.0",
|
|
14
29
|
"typescript": "^5.9.3"
|
|
15
30
|
},
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
31
|
+
"scripts": {
|
|
32
|
+
"b": "bun run build",
|
|
33
|
+
"build": "bun run build:ts && bun run build:es",
|
|
34
|
+
"build:es": "nano-build --preset=module src/main.ts",
|
|
35
|
+
"build:ts": "tsc --build",
|
|
36
|
+
"c": "bun run clean",
|
|
37
|
+
"cb": "bun run clean && bun run build",
|
|
38
|
+
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
39
|
+
"d": "bun run build:es && bun",
|
|
40
|
+
"t": "bun run test",
|
|
41
|
+
"test": "bun test",
|
|
42
|
+
"w": "bun run watch",
|
|
43
|
+
"watch": "bun run watch:ts & bun run watch:es",
|
|
44
|
+
"watch:es": "bun run build:es --watch",
|
|
45
|
+
"watch:ts": "bun run build:ts --watch --preserveWatchOutput"
|
|
22
46
|
},
|
|
23
47
|
"files": [
|
|
24
|
-
"**/*.{js,mjs,cjs,map,d.ts,html,
|
|
48
|
+
"**/*.{js,mjs,cjs,ts,map,d.ts,html,LEGAL.txt}",
|
|
49
|
+
"README.md",
|
|
25
50
|
"LICENSE",
|
|
26
51
|
"!demo/**/*",
|
|
27
52
|
"!**/*.test.js"
|
|
28
53
|
],
|
|
29
|
-
"
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
30
57
|
"keywords": [
|
|
31
58
|
"alwatr",
|
|
32
59
|
"array-shuffle",
|
|
@@ -57,36 +84,6 @@
|
|
|
57
84
|
"uuid",
|
|
58
85
|
"uuid-v4"
|
|
59
86
|
],
|
|
60
|
-
"license": "MPL-2.0",
|
|
61
|
-
"main": "./dist/main.cjs",
|
|
62
|
-
"module": "./dist/main.mjs",
|
|
63
87
|
"prettier": "@alwatr/prettier-config",
|
|
64
|
-
"
|
|
65
|
-
"access": "public"
|
|
66
|
-
},
|
|
67
|
-
"repository": {
|
|
68
|
-
"type": "git",
|
|
69
|
-
"url": "https://github.com/Alwatr/nanolib",
|
|
70
|
-
"directory": "packages/random"
|
|
71
|
-
},
|
|
72
|
-
"scripts": {
|
|
73
|
-
"b": "bun run build",
|
|
74
|
-
"build": "bun run build:ts && bun run build:es",
|
|
75
|
-
"build:es": "nano-build --preset=module",
|
|
76
|
-
"build:ts": "tsc --build",
|
|
77
|
-
"c": "bun run clean",
|
|
78
|
-
"cb": "bun run clean && bun run build",
|
|
79
|
-
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
80
|
-
"d": "bun run build:es && bun --enable-source-maps --trace-warnings",
|
|
81
|
-
"t": "bun run test",
|
|
82
|
-
"test": "bun test",
|
|
83
|
-
"w": "bun run watch",
|
|
84
|
-
"watch": "bun run watch:ts & bun run watch:es",
|
|
85
|
-
"watch:es": "bun run build:es --watch",
|
|
86
|
-
"watch:ts": "bun run build:ts --watch --preserveWatchOutput"
|
|
87
|
-
},
|
|
88
|
-
"sideEffects": false,
|
|
89
|
-
"type": "module",
|
|
90
|
-
"types": "./dist/main.d.ts",
|
|
91
|
-
"gitHead": "def1decf8f496f4b0d3d3ab952c346c689c30160"
|
|
88
|
+
"gitHead": "f843742e94d6ebed3921d57b624d9deb35c2c102"
|
|
92
89
|
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import {getGlobalThis} from '@alwatr/global-this';
|
|
2
|
+
|
|
3
|
+
const globalThis = getGlobalThis();
|
|
4
|
+
|
|
5
|
+
// Use the native crypto module when available for better randomness
|
|
6
|
+
const hasCrypto = (() => typeof globalThis.crypto !== 'undefined')();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts a Uint8Array or number array into a hexadecimal string representation.
|
|
10
|
+
* Each byte is converted to a two-character hex string (padded with a leading zero if necessary)
|
|
11
|
+
* and concatenated together to form a single string.
|
|
12
|
+
*
|
|
13
|
+
* @param bytes - The array of bytes to convert to hexadecimal
|
|
14
|
+
* @returns A hexadecimal string representation of the input bytes
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* // Using with Uint8Array
|
|
19
|
+
* const bytes = new Uint8Array([10, 255, 0, 16]);
|
|
20
|
+
* bytesToHex(bytes); // Returns "0aff0010"
|
|
21
|
+
*
|
|
22
|
+
* // Using with number array
|
|
23
|
+
* const array = [171, 205, 3];
|
|
24
|
+
* bytesToHex(array); // Returns "abcd03"
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function bytesToHex(bytes: number[] | Uint8Array): string {
|
|
28
|
+
let result = '';
|
|
29
|
+
for (const byte of bytes) {
|
|
30
|
+
const hex = byte.toString(16);
|
|
31
|
+
result += hex.length === 1 ? '0' + hex : hex;
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns a float random number between 0 and 1 (1 not included).
|
|
38
|
+
*
|
|
39
|
+
* Example:
|
|
40
|
+
*
|
|
41
|
+
* ```js
|
|
42
|
+
* console.log(randNumber()); // 0.7124123
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function randNumber(): number {
|
|
46
|
+
return Math.random();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Generate a random float number between min and max (max not included).
|
|
51
|
+
*
|
|
52
|
+
* Example:
|
|
53
|
+
*
|
|
54
|
+
* ```js
|
|
55
|
+
* console.log(randFloat(1, 10)); // somewhere between 1 and 10 (as float)
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export function randFloat(min: number, max: number): number {
|
|
59
|
+
return Math.random() * (max - min) + min;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Generate a random integer number between min and max (max included).
|
|
64
|
+
*
|
|
65
|
+
* Example:
|
|
66
|
+
*
|
|
67
|
+
* ```js
|
|
68
|
+
* console.log(randInteger(1, 10)); // somewhere between 1 and 10
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function randInteger(min: number, max: number): number {
|
|
72
|
+
// Use Math.floor and add 1 to max for better distribution
|
|
73
|
+
return Math.floor(randFloat(min, max + 1));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Generate a random string with specified length.
|
|
78
|
+
* The string will contain only characters from the characters list.
|
|
79
|
+
* The length of the string will be between min and max (max included).
|
|
80
|
+
* If max not specified, the length will be set to min.
|
|
81
|
+
*
|
|
82
|
+
* Example:
|
|
83
|
+
*
|
|
84
|
+
*```js
|
|
85
|
+
* console.log(randString(6)); // something like 'Aab1V2'
|
|
86
|
+
* console.log(randString(3, 6)); // random length between 3 and 6
|
|
87
|
+
* console.log(randString(5, undefined, '01')); // binary string like '10101'
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export function randString(
|
|
91
|
+
minLength: number,
|
|
92
|
+
maxLength: number = minLength,
|
|
93
|
+
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
|
94
|
+
): string {
|
|
95
|
+
const length = maxLength === minLength ? minLength : randInteger(minLength, maxLength);
|
|
96
|
+
if (length <= 0) return '';
|
|
97
|
+
|
|
98
|
+
const charsLength = chars.length;
|
|
99
|
+
|
|
100
|
+
let result = '';
|
|
101
|
+
|
|
102
|
+
// Small optimization for short strings
|
|
103
|
+
if (length <= 10) {
|
|
104
|
+
for (let i = 0; i < length; i++) {
|
|
105
|
+
result += chars.charAt(Math.floor(Math.random() * charsLength));
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
// else
|
|
110
|
+
// For longer strings, use array join for better performance
|
|
111
|
+
const resultArray = new Array(length);
|
|
112
|
+
for (let i = 0; i < length; i++) {
|
|
113
|
+
resultArray[i] = chars.charAt(Math.floor(Math.random() * charsLength));
|
|
114
|
+
}
|
|
115
|
+
return resultArray.join('');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Generate a random integer between min and max with a step.
|
|
120
|
+
*
|
|
121
|
+
* Example:
|
|
122
|
+
*
|
|
123
|
+
* ```js
|
|
124
|
+
* console.log(randStep(6, 10, 2)); // 6 or 8 or 10
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export function randStep(min: number, max: number, step: number): number {
|
|
128
|
+
if (step === 0) {
|
|
129
|
+
return min; // Return min when step is 0 to avoid division by zero
|
|
130
|
+
}
|
|
131
|
+
const steps = Math.floor((max - min) / step);
|
|
132
|
+
return min + randInteger(0, steps) * step;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Shuffle an array in place using Fisher-Yates shuffle algorithm and return it.
|
|
137
|
+
*
|
|
138
|
+
* Example:
|
|
139
|
+
*
|
|
140
|
+
* ```js
|
|
141
|
+
* const array = [1, 2, 3, 4, 5];
|
|
142
|
+
* randShuffle(array);
|
|
143
|
+
* console.log(array); // [2, 4, 3, 1, 5] (randomized)
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export function randShuffle<T>(array: T[]): T[] {
|
|
147
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
148
|
+
const j = randInteger(0, i);
|
|
149
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
150
|
+
}
|
|
151
|
+
return array;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Choose a random item from an array.
|
|
156
|
+
* Throws an error if the array is empty.
|
|
157
|
+
*
|
|
158
|
+
* Example:
|
|
159
|
+
*
|
|
160
|
+
* ```js
|
|
161
|
+
* const array = [1, 2, 3, 4, 5];
|
|
162
|
+
* console.log(randPick(array)); // one random element
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
export function randPick<T>(array: T[]): T {
|
|
166
|
+
if (array.length === 0) throw new Error('Cannot pick from empty array');
|
|
167
|
+
return array[randInteger(0, array.length - 1)];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Fills a typed array with random integer values within the specified range.
|
|
172
|
+
* The array is modified in place and also returned for chaining.
|
|
173
|
+
*
|
|
174
|
+
* @param array - The array to fill with random values (modified in place)
|
|
175
|
+
* @param min - Minimum value (inclusive), defaults to 0
|
|
176
|
+
* @param max - Maximum value (inclusive), defaults to 255
|
|
177
|
+
* @returns The same array that was passed in (for chaining)
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* // Fill a Uint8Array with random values (0-255)
|
|
182
|
+
* randArray(new Uint8Array(10));
|
|
183
|
+
*
|
|
184
|
+
* // Fill with custom range
|
|
185
|
+
* randArray(new Uint16Array(5), 1000, 2000); // Values between 1000-2000
|
|
186
|
+
*
|
|
187
|
+
* // Also works with number arrays
|
|
188
|
+
* randArray(new Array<number>(8), -100, 100); // Values between -100 and 100
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
export function randArray<T extends number[] | Uint8Array | Uint16Array | Uint32Array>(array: T, min = 0, max = 255): T {
|
|
192
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
193
|
+
array[i] = randInteger(min, max);
|
|
194
|
+
}
|
|
195
|
+
return array;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Type alias for a UUID string.
|
|
200
|
+
*/
|
|
201
|
+
export type UUID = `${string}-${string}-${string}-${string}-${string}`;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Generate a random UUID (v4).
|
|
205
|
+
*
|
|
206
|
+
* Example:
|
|
207
|
+
*
|
|
208
|
+
* ```js
|
|
209
|
+
* console.log(randUuid()); // "a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
export function randUuid(): UUID {
|
|
213
|
+
if (hasCrypto && globalThis.crypto?.randomUUID) {
|
|
214
|
+
return globalThis.crypto.randomUUID() as UUID;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Fallback implementation
|
|
218
|
+
const bytes = randArray(new Uint8Array(16));
|
|
219
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40; // version 4
|
|
220
|
+
bytes[8] = (bytes[8] & 0xbf) | 0x80; // variant RFC4122
|
|
221
|
+
|
|
222
|
+
// prettier-ignore
|
|
223
|
+
return `${
|
|
224
|
+
bytesToHex(bytes.subarray(0, 4))
|
|
225
|
+
}-${
|
|
226
|
+
bytesToHex(bytes.subarray(4, 6))
|
|
227
|
+
}-${
|
|
228
|
+
bytesToHex(bytes.subarray(6, 8))
|
|
229
|
+
}-${
|
|
230
|
+
bytesToHex(bytes.subarray(8, 10))
|
|
231
|
+
}-${
|
|
232
|
+
bytesToHex(bytes.subarray(10, 16))
|
|
233
|
+
}` as UUID;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Generate a random boolean with specified probability of being true.
|
|
238
|
+
*
|
|
239
|
+
* Example:
|
|
240
|
+
*
|
|
241
|
+
* ```js
|
|
242
|
+
* console.log(randBoolean()); // 50% chance of true
|
|
243
|
+
* console.log(randBoolean(0.8)); // 80% chance of true
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
export function randBoolean(probability = 0.5): boolean {
|
|
247
|
+
return Math.random() < probability;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Generate a random hex color string.
|
|
252
|
+
*
|
|
253
|
+
* Example:
|
|
254
|
+
*
|
|
255
|
+
* ```js
|
|
256
|
+
* console.log(randColor()); // "#a1b2c3"
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
export function randColor(): string {
|
|
260
|
+
const bytes = randArray(new Array<number>(3));
|
|
261
|
+
return `#${bytesToHex(bytes)}`;
|
|
262
|
+
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
# Change Log
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
-
|
|
6
|
-
## [5.1.31](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.30...@alwatr/random@5.1.31) (2026-03-18)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package @alwatr/random
|
|
9
|
-
|
|
10
|
-
## [5.1.30](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.29...@alwatr/random@5.1.30) (2026-03-16)
|
|
11
|
-
|
|
12
|
-
### 🔨 Code Refactoring
|
|
13
|
-
|
|
14
|
-
* migrate build scripts from yarn to bun across multiple packages ([d90e962](https://github.com/Alwatr/nanolib/commit/d90e962f15e5c951e191d5f02341279b6472abc3))
|
|
15
|
-
|
|
16
|
-
## [5.1.29](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.28...@alwatr/random@5.1.29) (2026-02-18)
|
|
17
|
-
|
|
18
|
-
**Note:** Version bump only for package @alwatr/random
|
|
19
|
-
|
|
20
|
-
## [5.1.28](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.27...@alwatr/random@5.1.28) (2025-12-23)
|
|
21
|
-
|
|
22
|
-
**Note:** Version bump only for package @alwatr/random
|
|
23
|
-
|
|
24
|
-
## [5.1.27](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.26...@alwatr/random@5.1.27) (2025-12-13)
|
|
25
|
-
|
|
26
|
-
**Note:** Version bump only for package @alwatr/random
|
|
27
|
-
|
|
28
|
-
## [5.1.26](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.25...@alwatr/random@5.1.26) (2025-12-10)
|
|
29
|
-
|
|
30
|
-
**Note:** Version bump only for package @alwatr/random
|
|
31
|
-
|
|
32
|
-
## [5.1.25](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.24...@alwatr/random@5.1.25) (2025-11-18)
|
|
33
|
-
|
|
34
|
-
### 🔨 Code Refactoring
|
|
35
|
-
|
|
36
|
-
* remove unnecessary type declarations from tsconfig.json files ([89bcc7d](https://github.com/Alwatr/nanolib/commit/89bcc7db839807110b80f8ba34414ea9734d9c75))
|
|
37
|
-
|
|
38
|
-
## [5.1.24](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.23...@alwatr/random@5.1.24) (2025-11-15)
|
|
39
|
-
|
|
40
|
-
**Note:** Version bump only for package @alwatr/random
|
|
41
|
-
|
|
42
|
-
## [5.1.23](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.22...@alwatr/random@5.1.23) (2025-11-15)
|
|
43
|
-
|
|
44
|
-
**Note:** Version bump only for package @alwatr/random
|
|
45
|
-
|
|
46
|
-
## [5.1.22](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.21...@alwatr/random@5.1.22) (2025-11-04)
|
|
47
|
-
|
|
48
|
-
**Note:** Version bump only for package @alwatr/random
|
|
49
|
-
|
|
50
|
-
## [5.1.21](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.20...@alwatr/random@5.1.21) (2025-10-06)
|
|
51
|
-
|
|
52
|
-
### 🔗 Dependencies update
|
|
53
|
-
|
|
54
|
-
* bump the npm-dependencies group with 4 updates ([9825815](https://github.com/Alwatr/nanolib/commit/982581552bbb4b97dca52af5e93a80937f0c3109))
|
|
55
|
-
|
|
56
|
-
## [5.1.20](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.19...@alwatr/random@5.1.20) (2025-09-27)
|
|
57
|
-
|
|
58
|
-
### 🧹 Miscellaneous Chores
|
|
59
|
-
|
|
60
|
-
* exclude test files from package distribution ([86f4f2f](https://github.com/Alwatr/nanolib/commit/86f4f2f5985845c5cf3a3a9398de7b2f98ce53e7))
|
|
61
|
-
|
|
62
|
-
## [5.1.19](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.18...@alwatr/random@5.1.19) (2025-09-22)
|
|
63
|
-
|
|
64
|
-
**Note:** Version bump only for package @alwatr/random
|
|
65
|
-
|
|
66
|
-
## [5.1.18](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.17...@alwatr/random@5.1.18) (2025-09-22)
|
|
67
|
-
|
|
68
|
-
**Note:** Version bump only for package @alwatr/random
|
|
69
|
-
|
|
70
|
-
## [5.1.17](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.16...@alwatr/random@5.1.17) (2025-09-21)
|
|
71
|
-
|
|
72
|
-
**Note:** Version bump only for package @alwatr/random
|
|
73
|
-
|
|
74
|
-
## [5.1.16](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.15...@alwatr/random@5.1.16) (2025-09-20)
|
|
75
|
-
|
|
76
|
-
### 🐛 Bug Fixes
|
|
77
|
-
|
|
78
|
-
* add sideEffects property to package.json files for better tree-shaking ([c7b9e74](https://github.com/Alwatr/nanolib/commit/c7b9e74e1920c8e35b438742de61883ca62da58c))
|
|
79
|
-
* add sideEffects property to package.json files for better tree-shaking ([e8402c4](https://github.com/Alwatr/nanolib/commit/e8402c481a14a1f807a37aaa862a936713d26176))
|
|
80
|
-
* remove unnecessary pure annotations ([adeb916](https://github.com/Alwatr/nanolib/commit/adeb9166f8e911f59269032b76c36cb1888332cf))
|
|
81
|
-
|
|
82
|
-
### 🧹 Miscellaneous Chores
|
|
83
|
-
|
|
84
|
-
* remove duplicate sideEffects property from multiple package.json files ([b123f86](https://github.com/Alwatr/nanolib/commit/b123f86be81481de2314aae9bb2eeb629743d24c))
|
|
85
|
-
|
|
86
|
-
## [5.1.15](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.14...@alwatr/random@5.1.15) (2025-09-19)
|
|
87
|
-
|
|
88
|
-
**Note:** Version bump only for package @alwatr/random
|
|
89
|
-
|
|
90
|
-
## [5.1.14](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.13...@alwatr/random@5.1.14) (2025-09-15)
|
|
91
|
-
|
|
92
|
-
**Note:** Version bump only for package @alwatr/random
|
|
93
|
-
|
|
94
|
-
## [5.1.13](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.12...@alwatr/random@5.1.13) (2025-09-14)
|
|
95
|
-
|
|
96
|
-
**Note:** Version bump only for package @alwatr/random
|
|
97
|
-
|
|
98
|
-
## [5.1.12](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.11...@alwatr/random@5.1.12) (2025-09-13)
|
|
99
|
-
|
|
100
|
-
**Note:** Version bump only for package @alwatr/random
|
|
101
|
-
|
|
102
|
-
## [5.1.11](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.10...@alwatr/random@5.1.11) (2025-09-13)
|
|
103
|
-
|
|
104
|
-
### 🧹 Miscellaneous Chores
|
|
105
|
-
|
|
106
|
-
* remove package-tracer dependency and related code from fetch package ([96fe4e9](https://github.com/Alwatr/nanolib/commit/96fe4e9552a205f218ceed187c55e4e904a07089))
|
|
107
|
-
|
|
108
|
-
## [5.1.10](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.9...@alwatr/random@5.1.10) (2025-09-09)
|
|
109
|
-
|
|
110
|
-
### 🧹 Miscellaneous Chores
|
|
111
|
-
|
|
112
|
-
* remove trailing newlines from contributing sections in README files ([e8ab1bc](https://github.com/Alwatr/nanolib/commit/e8ab1bc43e0addea5ccd4c897c2cec597cb9e15f))
|
|
113
|
-
|
|
114
|
-
## [5.1.9](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.8...@alwatr/random@5.1.9) (2025-09-08)
|
|
115
|
-
|
|
116
|
-
### 🔨 Code Refactoring
|
|
117
|
-
|
|
118
|
-
* **main:** mark getGlobalThis and hasCrypto as pure for optimization ([8474cc2](https://github.com/Alwatr/nanolib/commit/8474cc247ef0a7df0975c72a5c1ab8240844b810))
|
|
119
|
-
|
|
120
|
-
## [5.1.8](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.7...@alwatr/random@5.1.8) (2025-09-06)
|
|
121
|
-
|
|
122
|
-
**Note:** Version bump only for package @alwatr/random
|
|
123
|
-
|
|
124
|
-
## [5.1.7](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.6...@alwatr/random@5.1.7) (2025-09-05)
|
|
125
|
-
|
|
126
|
-
### 🔗 Dependencies update
|
|
127
|
-
|
|
128
|
-
* update jest to version 30.1.3 and @types/node to version 22.18.1 ([754212b](https://github.com/Alwatr/nanolib/commit/754212b1523cfc4cfe26c9e9f6d634aa8311e0b7))
|
|
129
|
-
|
|
130
|
-
## [5.1.6](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.5...@alwatr/random@5.1.6) (2025-09-01)
|
|
131
|
-
|
|
132
|
-
### 🔗 Dependencies update
|
|
133
|
-
|
|
134
|
-
* update lerna-lite dependencies to version 4.7.3 and jest to 30.1.2 ([95d7870](https://github.com/Alwatr/nanolib/commit/95d7870ec7ad1e6ed2688bafddcabf46857f6981))
|
|
135
|
-
|
|
136
|
-
## [5.1.5](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.4...@alwatr/random@5.1.5) (2025-08-23)
|
|
137
|
-
|
|
138
|
-
**Note:** Version bump only for package @alwatr/random
|
|
139
|
-
|
|
140
|
-
## [5.1.4](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.2...@alwatr/random@5.1.4) (2025-08-23)
|
|
141
|
-
|
|
142
|
-
### 🐛 Bug Fixes
|
|
143
|
-
|
|
144
|
-
* update license from AGPL-3.0-only to MPL-2.0 ([d20968e](https://github.com/Alwatr/nanolib/commit/d20968e60cc89b1dcdf9b96507178da6ed562f55))
|
|
145
|
-
* update package versions in multiple package.json files ([7638b1c](https://github.com/Alwatr/nanolib/commit/7638b1cafee2b4e0f97db7a89ac9fba6384b9b10))
|
|
146
|
-
|
|
147
|
-
### 🔨 Code Refactoring
|
|
148
|
-
|
|
149
|
-
* Updated all package.json files in the project to change dependency version specifiers from "workspace:^" to "workspace:*" for consistency and to allow for more flexible version resolution. ([db6a4f7](https://github.com/Alwatr/nanolib/commit/db6a4f76deec2d1d8039978144e4bc51b6f1a0e3))
|
|
150
|
-
|
|
151
|
-
### 🧹 Miscellaneous Chores
|
|
152
|
-
|
|
153
|
-
* reformat all package.json files ([ceda45d](https://github.com/Alwatr/nanolib/commit/ceda45de186667790474f729cb4b161a5148ce19))
|
|
154
|
-
|
|
155
|
-
### 🔗 Dependencies update
|
|
156
|
-
|
|
157
|
-
* update TypeScript and Jest versions across all packages to improve compatibility and performance ([31baf36](https://github.com/Alwatr/nanolib/commit/31baf366101e92e27db66a21c849fb101f19be47))
|
|
158
|
-
|
|
159
|
-
## [5.1.3](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.2...@alwatr/random@5.1.3) (2025-08-23)
|
|
160
|
-
|
|
161
|
-
### Code Refactoring
|
|
162
|
-
|
|
163
|
-
* Updated all package.json files in the project to change dependency version specifiers from "workspace:^" to "workspace:*" for consistency and to allow for more flexible version resolution. ([db6a4f7](https://github.com/Alwatr/nanolib/commit/db6a4f76deec2d1d8039978144e4bc51b6f1a0e3)) by @alimd
|
|
164
|
-
|
|
165
|
-
## <small>5.1.2 (2025-04-15)</small>
|
|
166
|
-
|
|
167
|
-
**Note:** Version bump only for package @alwatr/random
|
|
168
|
-
|
|
169
|
-
## [5.1.1](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.0...@alwatr/random@5.1.1) (2025-04-01)
|
|
170
|
-
|
|
171
|
-
**Note:** Version bump only for package @alwatr/random
|
|
172
|
-
|
|
173
|
-
## 5.1.0 (2025-03-18)
|
|
174
|
-
|
|
175
|
-
### Features
|
|
176
|
-
|
|
177
|
-
* **random:** add hex function to convert Uint8Array to hexadecimal string ([61125e7](https://github.com/Alwatr/nanolib/commit/61125e7cf8b87094e026f578f8141df6ea5153f8)) by @alimd
|
|
178
|
-
* **random:** add package.json for @alwatr/random utility library ([4c02a70](https://github.com/Alwatr/nanolib/commit/4c02a70a46a740ca7480769f8857880db296662a)) by @alimd
|
|
179
|
-
* **random:** add randBoolean function to generate random boolean values with specified probability ([22ceca5](https://github.com/Alwatr/nanolib/commit/22ceca51af9e41bcd179a457a418736548865551)) by @alimd
|
|
180
|
-
* **random:** add randColor function to generate random hex color strings ([1bde087](https://github.com/Alwatr/nanolib/commit/1bde0873459047e3ac9b11fe22a42600780daaca)) by @alimd
|
|
181
|
-
* **random:** add randFloat function to generate a random float between specified min and max values ([1c7862a](https://github.com/Alwatr/nanolib/commit/1c7862af5239eb76589f537184b82bcf098f377b)) by @alimd
|
|
182
|
-
* **random:** add randInteger function to generate a random integer between specified min and max values ([d9bf2c4](https://github.com/Alwatr/nanolib/commit/d9bf2c4c0bfc39030007679e3cf1a8ef90016b09)) by @alimd
|
|
183
|
-
* **random:** add randPick function to select a random item from an array ([b4d9620](https://github.com/Alwatr/nanolib/commit/b4d962021f7cd180e89b4a32f2a7c85367e61cf6)) by @alimd
|
|
184
|
-
* **random:** add randShuffle function to shuffle an array in place ([4124ec5](https://github.com/Alwatr/nanolib/commit/4124ec5e1ba2ec0915a91623333ef3b2d575fe5a)) by @alimd
|
|
185
|
-
* **random:** add randStep function to generate a random integer between min and max with a specified step ([52bddca](https://github.com/Alwatr/nanolib/commit/52bddca00927780d28acb41e56f9558003a71476)) by @alimd
|
|
186
|
-
* **random:** add randString function to generate a random string of specified length ([92fd657](https://github.com/Alwatr/nanolib/commit/92fd65764d370209f7e2115d1bee85e7da753a1f)) by @alimd
|
|
187
|
-
* **random:** add randUuid function to generate random UUIDs (v4) ([805a941](https://github.com/Alwatr/nanolib/commit/805a9413fb9080e9bd90b162cbbbe9907c462d4a)) by @alimd
|
|
188
|
-
* **random:** add randValues function to fill a typed array with cryptographically strong random values ([a3274c6](https://github.com/Alwatr/nanolib/commit/a3274c6c1a532ea05b4c9c0c9d7d98f9b452f37c)) by @alimd
|
|
189
|
-
* **random:** add TypeScript configuration for @alwatr/random package ([6e8c85e](https://github.com/Alwatr/nanolib/commit/6e8c85ee358149ad1db17b432d4135fa38bc1488)) by @alimd
|
|
190
|
-
* **random:** add UUID type alias for UUID string representation ([4f23d80](https://github.com/Alwatr/nanolib/commit/4f23d802672731d8700cf6808dd6bd7b18d3c0c4)) by @alimd
|
|
191
|
-
* **random:** enhance randString function to support custom character sets and update documentation ([f8bc2d7](https://github.com/Alwatr/nanolib/commit/f8bc2d7091338cdccaa08b423c62f7797ffd21ef)) by @alimd
|
|
192
|
-
* **random:** implement randNumber function to generate a random float between 0 and 1 ([6b06f45](https://github.com/Alwatr/nanolib/commit/6b06f4559334fc06d6987f4d76b04b6584c5f9b1)) by @alimd
|
|
193
|
-
|
|
194
|
-
### Bug Fixes
|
|
195
|
-
|
|
196
|
-
* downgrade version of hash-string and random packages to 5.0.0 ([176fc5c](https://github.com/Alwatr/nanolib/commit/176fc5c6a5af37d8b7cb7f41336910f0e8fcd0a9)) by @
|
|
197
|
-
* **random:** initialize globalThis variable using getGlobalThis function ([191a827](https://github.com/Alwatr/nanolib/commit/191a827232762a435cc6b87f1cee31c0f1a8fc46)) by @alimd
|
|
198
|
-
|
|
199
|
-
### Code Refactoring
|
|
200
|
-
|
|
201
|
-
* **random:** improve randShuffle function to use Fisher-Yates algorithm for better shuffling ([3226a60](https://github.com/Alwatr/nanolib/commit/3226a605ac5b6211f82897e3e691347b30a69efa)) by @alimd
|
|
202
|
-
* **random:** rename hex function to bytesToHex and update documentation for clarity ([3fcb327](https://github.com/Alwatr/nanolib/commit/3fcb327b41a5016cd5e7e7b5945f5b36383b3ab7)) by @alimd
|
|
203
|
-
* **random:** rename randValues to randArray and update documentation for clarity and functionality ([48f8bd4](https://github.com/Alwatr/nanolib/commit/48f8bd469fe48a4db9cd0768cd2897cc6ad4f8a2)) by @alimd
|
|
204
|
-
* **random:** replace randNumber with Math.random for improved clarity and performance ([c6227d5](https://github.com/Alwatr/nanolib/commit/c6227d5ac9217d8fdf89501673f52fa44464ef19)) by @alimd
|
|
205
|
-
* **random:** replace randValues with randArray in randUuid function for consistency ([7b6fb37](https://github.com/Alwatr/nanolib/commit/7b6fb378c7705f8cf1604287638e482a33ae6d5e)) by @alimd
|
|
206
|
-
* **random:** update bytesToHex function to accept number arrays and improve documentation ([6fef474](https://github.com/Alwatr/nanolib/commit/6fef474d3cb5aa6d10adcac37c1fc5256ca62605)) by @alimd
|
|
207
|
-
|
|
208
|
-
### Miscellaneous Chores
|
|
209
|
-
|
|
210
|
-
* **random:** add @alwatr/global-this dependency to package.json ([23856a1](https://github.com/Alwatr/nanolib/commit/23856a151b959a237a2e9a011cf6fb08a2f36eb1)) by @alimd
|
package/dist/main.cjs
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
/** 📦 @alwatr/random v5.1.31 */
|
|
2
|
-
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var main_exports={};__export(main_exports,{bytesToHex:()=>bytesToHex,randArray:()=>randArray,randBoolean:()=>randBoolean,randColor:()=>randColor,randFloat:()=>randFloat,randInteger:()=>randInteger,randNumber:()=>randNumber,randPick:()=>randPick,randShuffle:()=>randShuffle,randStep:()=>randStep,randString:()=>randString,randUuid:()=>randUuid});module.exports=__toCommonJS(main_exports);var import_global_this=require("@alwatr/global-this");var globalThis=(0,import_global_this.getGlobalThis)();var hasCrypto=(()=>typeof globalThis.crypto!=="undefined")();function bytesToHex(bytes){let result="";for(const byte of bytes){const hex=byte.toString(16);result+=hex.length===1?"0"+hex:hex}return result}function randNumber(){return Math.random()}function randFloat(min,max){return Math.random()*(max-min)+min}function randInteger(min,max){return Math.floor(randFloat(min,max+1))}function randString(minLength,maxLength=minLength,chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"){const length=maxLength===minLength?minLength:randInteger(minLength,maxLength);if(length<=0)return"";const charsLength=chars.length;let result="";if(length<=10){for(let i=0;i<length;i++){result+=chars.charAt(Math.floor(Math.random()*charsLength))}return result}const resultArray=new Array(length);for(let i=0;i<length;i++){resultArray[i]=chars.charAt(Math.floor(Math.random()*charsLength))}return resultArray.join("")}function randStep(min,max,step){if(step===0){return min}const steps=Math.floor((max-min)/step);return min+randInteger(0,steps)*step}function randShuffle(array){for(let i=array.length-1;i>0;i--){const j=randInteger(0,i);[array[i],array[j]]=[array[j],array[i]]}return array}function randPick(array){if(array.length===0)throw new Error("Cannot pick from empty array");return array[randInteger(0,array.length-1)]}function randArray(array,min=0,max=255){for(let i=array.length-1;i>=0;i--){array[i]=randInteger(min,max)}return array}function randUuid(){if(hasCrypto&&globalThis.crypto?.randomUUID){return globalThis.crypto.randomUUID()}const bytes=randArray(new Uint8Array(16));bytes[6]=bytes[6]&15|64;bytes[8]=bytes[8]&191|128;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))}`}function randBoolean(probability=.5){return Math.random()<probability}function randColor(){const bytes=randArray(new Array(3));return`#${bytesToHex(bytes)}`}0&&(module.exports={bytesToHex,randArray,randBoolean,randColor,randFloat,randInteger,randNumber,randPick,randShuffle,randStep,randString,randUuid});
|
|
3
|
-
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/main.ts"],
|
|
4
|
-
"sourcesContent": ["import {getGlobalThis} from '@alwatr/global-this';\n\nconst globalThis = getGlobalThis();\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)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
|
6
|
-
"names": []
|
|
7
|
-
}
|
package/dist/main.mjs
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
/** 📦 @alwatr/random v5.1.31 */
|
|
2
|
-
import{getGlobalThis}from"@alwatr/global-this";var globalThis=getGlobalThis();var hasCrypto=(()=>typeof globalThis.crypto!=="undefined")();function bytesToHex(bytes){let result="";for(const byte of bytes){const hex=byte.toString(16);result+=hex.length===1?"0"+hex:hex}return result}function randNumber(){return Math.random()}function randFloat(min,max){return Math.random()*(max-min)+min}function randInteger(min,max){return Math.floor(randFloat(min,max+1))}function randString(minLength,maxLength=minLength,chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"){const length=maxLength===minLength?minLength:randInteger(minLength,maxLength);if(length<=0)return"";const charsLength=chars.length;let result="";if(length<=10){for(let i=0;i<length;i++){result+=chars.charAt(Math.floor(Math.random()*charsLength))}return result}const resultArray=new Array(length);for(let i=0;i<length;i++){resultArray[i]=chars.charAt(Math.floor(Math.random()*charsLength))}return resultArray.join("")}function randStep(min,max,step){if(step===0){return min}const steps=Math.floor((max-min)/step);return min+randInteger(0,steps)*step}function randShuffle(array){for(let i=array.length-1;i>0;i--){const j=randInteger(0,i);[array[i],array[j]]=[array[j],array[i]]}return array}function randPick(array){if(array.length===0)throw new Error("Cannot pick from empty array");return array[randInteger(0,array.length-1)]}function randArray(array,min=0,max=255){for(let i=array.length-1;i>=0;i--){array[i]=randInteger(min,max)}return array}function randUuid(){if(hasCrypto&&globalThis.crypto?.randomUUID){return globalThis.crypto.randomUUID()}const bytes=randArray(new Uint8Array(16));bytes[6]=bytes[6]&15|64;bytes[8]=bytes[8]&191|128;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))}`}function randBoolean(probability=.5){return Math.random()<probability}function randColor(){const bytes=randArray(new Array(3));return`#${bytesToHex(bytes)}`}export{bytesToHex,randArray,randBoolean,randColor,randFloat,randInteger,randNumber,randPick,randShuffle,randStep,randString,randUuid};
|
|
3
|
-
//# sourceMappingURL=main.mjs.map
|
package/dist/main.mjs.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/main.ts"],
|
|
4
|
-
"sourcesContent": ["import {getGlobalThis} from '@alwatr/global-this';\n\nconst globalThis = getGlobalThis();\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,OAAQ,kBAAoB,sBAE5B,IAAM,WAAa,cAAc,EAGjC,IAAM,WAAa,IAAM,OAAO,WAAW,SAAW,aAAa,EAqB5D,SAAS,WAAW,MAAsC,CAC/D,IAAI,OAAS,GACb,UAAW,QAAQ,MAAO,CACxB,MAAM,IAAM,KAAK,SAAS,EAAE,EAC5B,QAAU,IAAI,SAAW,EAAI,IAAM,IAAM,GAC3C,CACA,OAAO,MACT,CAWO,SAAS,YAAqB,CACnC,OAAO,KAAK,OAAO,CACrB,CAWO,SAAS,UAAU,IAAa,IAAqB,CAC1D,OAAO,KAAK,OAAO,GAAK,IAAM,KAAO,GACvC,CAWO,SAAS,YAAY,IAAa,IAAqB,CAE5D,OAAO,KAAK,MAAM,UAAU,IAAK,IAAM,CAAC,CAAC,CAC3C,CAgBO,SAAS,WACd,UACA,UAAoB,UACpB,MAAQ,iEACA,CACR,MAAM,OAAS,YAAc,UAAY,UAAY,YAAY,UAAW,SAAS,EACrF,GAAI,QAAU,EAAG,MAAO,GAExB,MAAM,YAAc,MAAM,OAE1B,IAAI,OAAS,GAGb,GAAI,QAAU,GAAI,CAChB,QAAS,EAAI,EAAG,EAAI,OAAQ,IAAK,CAC/B,QAAU,MAAM,OAAO,KAAK,MAAM,KAAK,OAAO,EAAI,WAAW,CAAC,CAChE,CACA,OAAO,MACT,CAGA,MAAM,YAAc,IAAI,MAAM,MAAM,EACpC,QAAS,EAAI,EAAG,EAAI,OAAQ,IAAK,CAC/B,YAAY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