@alwatr/random 6.0.0 → 6.0.2
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 +2 -2
- package/dist/main.js.map +1 -1
- package/package.json +39 -40
package/dist/main.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/* 📦 @alwatr/random v6.0.
|
|
1
|
+
/* 📦 @alwatr/random v6.0.2 */
|
|
2
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
3
|
|
|
4
|
-
//# debugId=
|
|
4
|
+
//# debugId=4C6B4423F53A318E64756E2164756E21
|
|
5
5
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -5,6 +5,6 @@
|
|
|
5
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
6
|
],
|
|
7
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,OAAO,EAIT,IAAM,EAAkB,MAAM,CAAM,EACpC,QAAS,EAAI,EAAG,EAAI,EAAQ,IAC1B,EAAY,GAAK,EAAM,OAAO,KAAK,MAAM,KAAK,OAAO,EAAI,CAAW,CAAC,EAEvE,OAAO,E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EAAU,KAAoB,EAC5C,MAAO,IAAI,EAAW,CAAK",
|
|
8
|
-
"debugId": "
|
|
8
|
+
"debugId": "4C6B4423F53A318E64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,24 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/random",
|
|
3
|
+
"version": "6.0.2",
|
|
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>",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"@alwatr/nano-build": "7.0.0",
|
|
12
|
-
"@alwatr/prettier-config": "7.0.0",
|
|
13
|
-
"@alwatr/tsconfig-base": "7.0.0",
|
|
14
|
-
"typescript": "^5.9.3"
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"directory": "packages/random",
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/Alwatr/nanolib"
|
|
15
12
|
},
|
|
13
|
+
"homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/random#readme",
|
|
14
|
+
"bugs": "https://github.com/Alwatr/nanolib/issues",
|
|
16
15
|
"exports": {
|
|
17
16
|
".": {
|
|
18
17
|
"types": "./dist/main.d.ts",
|
|
19
18
|
"default": "./dist/main.js"
|
|
20
19
|
}
|
|
21
20
|
},
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@alwatr/global-this": "6.0.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@alwatr/nano-build": "7.0.1",
|
|
27
|
+
"@alwatr/prettier-config": "7.0.1",
|
|
28
|
+
"@alwatr/tsconfig-base": "8.0.0",
|
|
29
|
+
"typescript": "^6.0.2"
|
|
30
|
+
},
|
|
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"
|
|
46
|
+
},
|
|
22
47
|
"files": [
|
|
23
48
|
"**/*.{js,mjs,cjs,ts,map,d.ts,html,LEGAL.txt}",
|
|
24
49
|
"README.md",
|
|
@@ -26,7 +51,9 @@
|
|
|
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,34 +84,6 @@
|
|
|
57
84
|
"uuid",
|
|
58
85
|
"uuid-v4"
|
|
59
86
|
],
|
|
60
|
-
"license": "MPL-2.0",
|
|
61
87
|
"prettier": "@alwatr/prettier-config",
|
|
62
|
-
"
|
|
63
|
-
"access": "public"
|
|
64
|
-
},
|
|
65
|
-
"repository": {
|
|
66
|
-
"type": "git",
|
|
67
|
-
"url": "https://github.com/Alwatr/nanolib",
|
|
68
|
-
"directory": "packages/random"
|
|
69
|
-
},
|
|
70
|
-
"scripts": {
|
|
71
|
-
"b": "bun run build",
|
|
72
|
-
"build": "bun run build:ts && bun run build:es",
|
|
73
|
-
"build:es": "nano-build --preset=module src/main.ts",
|
|
74
|
-
"build:ts": "tsc --build",
|
|
75
|
-
"c": "bun run clean",
|
|
76
|
-
"cb": "bun run clean && bun run build",
|
|
77
|
-
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
78
|
-
"d": "bun run build:es && bun",
|
|
79
|
-
"t": "bun run test",
|
|
80
|
-
"test": "bun test",
|
|
81
|
-
"w": "bun run watch",
|
|
82
|
-
"watch": "bun run watch:ts & bun run watch:es",
|
|
83
|
-
"watch:es": "bun run build:es --watch",
|
|
84
|
-
"watch:ts": "bun run build:ts --watch --preserveWatchOutput"
|
|
85
|
-
},
|
|
86
|
-
"sideEffects": false,
|
|
87
|
-
"type": "module",
|
|
88
|
-
"types": "./dist/main.d.ts",
|
|
89
|
-
"gitHead": "056102a1c8a563bbae8a290b6830450f467322a3"
|
|
88
|
+
"gitHead": "95bb13efd0a5f485c6b09f1a911b99492017aed1"
|
|
90
89
|
}
|