randomcryp 1.0.6 → 2.0.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/README.md +20 -19
- package/dist/index.d.ts +4 -2
- package/dist/index.js +23 -3
- package/dist/lib/hex.d.ts +7 -0
- package/dist/lib/shuffle.d.ts +7 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# randomcryp
|
|
2
2
|
|
|
3
|
-
A cryptographically secure, feature rich, zero dependency, lightweight and browser friendly random number generator library. Only ~
|
|
3
|
+
A cryptographically secure, feature rich, zero dependency, lightweight and browser friendly random number generator library. Only ~5KB in size.
|
|
4
4
|
Uses [Crypto.getRandomValues()](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) to get its randomness.
|
|
5
5
|
The spelling is random-creep in case you are wondering.
|
|
6
6
|
|
|
@@ -40,22 +40,23 @@ rangeInt(1, 10); // 7
|
|
|
40
40
|
|
|
41
41
|
#### List of Methods
|
|
42
42
|
|
|
43
|
-
| Method
|
|
44
|
-
|
|
|
45
|
-
| **`bool(): boolean`**
|
|
46
|
-
| **`boolean(): boolean`**
|
|
47
|
-
| **`percentage(val: number): boolean`**
|
|
48
|
-
| **`uSafeInt(): number`**
|
|
49
|
-
| **`float(): number`**
|
|
50
|
-
| **`random(): number`**
|
|
51
|
-
| **`
|
|
52
|
-
| **`
|
|
53
|
-
| **`
|
|
54
|
-
| **`
|
|
55
|
-
| **`
|
|
56
|
-
| **`
|
|
57
|
-
| **`
|
|
58
|
-
| **`
|
|
59
|
-
|
|
60
|
-
|
|
43
|
+
| Method | Description | Aliases |
|
|
44
|
+
| -- | -- | -- |
|
|
45
|
+
| **`bool(): boolean`** | Generates a random boolean (`true` or `false`). | `bool()` → `true` |
|
|
46
|
+
| **`boolean(): boolean`** | Alias for `bool()`. | `boolean()` → `false` |
|
|
47
|
+
| **`percentage(val: number): boolean`** | Generates `true` at given percentage of time. | `percentage(20)` → `false` |
|
|
48
|
+
| **`uSafeInt(): number`** | Generates a random integer between `0` (inclusive) and `Number.MAX_SAFE_INTEGER` (inclusive). | `uSafeInt()` → `4946544243668033` |
|
|
49
|
+
| **`float(): number`** | Generates a random number between `0` (inclusive) and `1` (exclusive). | `number()` → `0.190088246732104` |
|
|
50
|
+
| **`random(): number`** | Alias for `float()`. | `random()` → `0.9520779718919631` |
|
|
51
|
+
| **`hex(length: number = 8, prefix: boolean = false): string`** | Generates a random hex string of the specified length (default 8). Optionally prefixes with '0x'. | `hex(16)` → `d1ef0149c7849844` |
|
|
52
|
+
| **`choice(arr: ArrayLike<E>): E`** | Selects a random element from an array. | `choice([1, 2, 3, 4, 5])` → `3` |
|
|
53
|
+
| **`pick(arr: ArrayLike<E>): E`** | Alias for `choice()`. | `pick([1, 2, 3, 4, 5])` → `1` |
|
|
54
|
+
| **`shuffle(input: string): string`**, **`shuffle(input: Array<E>): Array<E>`** | Returns a new array or string after shuffling the given array or string. | `shuffle([1, 2, 3, 4, 5])` → `[ 1, 3, 2, 5, 4 ]` |
|
|
55
|
+
| **`range(min: number, max: number): number`** | Generates a random number (not integer) between given `min` (inclusive) and `max` (exclusive). Throws if `min` > `max`. | `range(1, 5)` → `4.103370176158448` |
|
|
56
|
+
| **`rangeInt(min: number, max: number): number`** | Generates a random number (not integer) between given `min` (inclusive) and `max` (exclusive). Throws if `min` > `max`. | `rangeInt(1, 10)` → `8` |
|
|
57
|
+
| **`randInt(min: number, max: number): number`** | Alias for `rangeInt()`. | `randInt(1, 100)` → `35` |
|
|
58
|
+
| **`safeInt(): number`** | Generates a random integer between `Number.MIN_SAFE_INTEGER` (inclusive) and `Number.MAX_SAFE_INTEGER` (inclusive). +0 and -0 both can be generated. 54 bits precision. Not recommended for genral usage. | `safeInt()` → `-5802548511349229` |
|
|
59
|
+
| **`ifloat(): number`** | Generates a random number between `-1` (inclusive) and `1` (inclusive). Uses `safeInt()` and thus not recommended. | `ifloat()` → `-0.6076475248861822` |
|
|
60
|
+
|
|
61
|
+
© 2025, Md. Touhidur Rahman.
|
|
61
62
|
License: BSD-3-Clause.
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { bool } from "./lib/bool";
|
|
|
2
2
|
import { boolean } from "./lib/boolean";
|
|
3
3
|
import { choice } from "./lib/choice";
|
|
4
4
|
import { float } from "./lib/float";
|
|
5
|
+
import { hex } from "./lib/hex";
|
|
5
6
|
import { ifloat } from "./lib/ifloat";
|
|
6
7
|
import { percentage } from "./lib/percentage";
|
|
7
8
|
import { pick } from "./lib/pick";
|
|
@@ -12,12 +13,13 @@ import { rangeInt } from "./lib/rangeInt";
|
|
|
12
13
|
import { safeInt } from "./lib/safeInt";
|
|
13
14
|
import { shuffle } from "./lib/shuffle";
|
|
14
15
|
import { uSafeInt } from "./lib/uSafeInt";
|
|
15
|
-
export { bool, boolean, choice, float, ifloat, percentage, pick, randInt, random, range, rangeInt, safeInt, shuffle, uSafeInt, };
|
|
16
|
+
export { bool, boolean, choice, float, hex, ifloat, percentage, pick, randInt, random, range, rangeInt, safeInt, shuffle, uSafeInt, };
|
|
16
17
|
declare const _default: {
|
|
17
18
|
bool: () => boolean;
|
|
18
19
|
boolean: () => boolean;
|
|
19
20
|
choice: <E>(choices: ArrayLike<E>) => E | undefined;
|
|
20
21
|
float: () => number;
|
|
22
|
+
hex: (length?: number, prefix?: boolean) => string;
|
|
21
23
|
ifloat: () => number;
|
|
22
24
|
percentage: (input: number) => boolean;
|
|
23
25
|
pick: <E>(choices: ArrayLike<E>) => E | undefined;
|
|
@@ -26,7 +28,7 @@ declare const _default: {
|
|
|
26
28
|
range: (min: number, max: number) => number;
|
|
27
29
|
rangeInt: (min: number, max: number) => number;
|
|
28
30
|
safeInt: () => number;
|
|
29
|
-
shuffle:
|
|
31
|
+
shuffle: typeof shuffle;
|
|
30
32
|
uSafeInt: () => number;
|
|
31
33
|
};
|
|
32
34
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,23 @@ var choice = (choices) => {
|
|
|
20
20
|
return choices[Math.floor(float() * choices.length)];
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
// src/lib/hex.ts
|
|
24
|
+
var digits = "0123456789abcdef";
|
|
25
|
+
var hex = (length = 8, prefix = false) => {
|
|
26
|
+
const randArr = crypto.getRandomValues(new Uint8Array(Math.ceil(length / 2)));
|
|
27
|
+
const hexArr = new Array(randArr.length);
|
|
28
|
+
for (let i = 0, j = 0;i < length; i++, j = Math.floor(i / 2)) {
|
|
29
|
+
if (i % 2 === 0) {
|
|
30
|
+
hexArr[i] = digits[randArr[j] >> 4];
|
|
31
|
+
} else {
|
|
32
|
+
hexArr[i] = digits[randArr[j] & 15];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (prefix)
|
|
36
|
+
return "0x" + hexArr.join("");
|
|
37
|
+
return hexArr.join("");
|
|
38
|
+
};
|
|
39
|
+
|
|
23
40
|
// src/lib/safeInt.ts
|
|
24
41
|
var POW_2_372 = Math.pow(2, 37);
|
|
25
42
|
var POW_2_212 = Math.pow(2, 21);
|
|
@@ -55,15 +72,16 @@ var range = (min, max) => {
|
|
|
55
72
|
};
|
|
56
73
|
|
|
57
74
|
// src/lib/shuffle.ts
|
|
58
|
-
|
|
75
|
+
function shuffle(input) {
|
|
76
|
+
const array = Array.from(input);
|
|
59
77
|
for (let i = array.length - 1;i > 0; i--) {
|
|
60
78
|
const pick = Math.floor(float() * (i + 1));
|
|
61
79
|
const temp = array[i];
|
|
62
80
|
array[i] = array[pick];
|
|
63
81
|
array[pick] = temp;
|
|
64
82
|
}
|
|
65
|
-
return array;
|
|
66
|
-
}
|
|
83
|
+
return typeof input === "string" ? array.join("") : array;
|
|
84
|
+
}
|
|
67
85
|
|
|
68
86
|
// src/index.ts
|
|
69
87
|
var src_default = {
|
|
@@ -71,6 +89,7 @@ var src_default = {
|
|
|
71
89
|
boolean: bool,
|
|
72
90
|
choice,
|
|
73
91
|
float,
|
|
92
|
+
hex,
|
|
74
93
|
ifloat,
|
|
75
94
|
percentage,
|
|
76
95
|
pick: choice,
|
|
@@ -93,6 +112,7 @@ export {
|
|
|
93
112
|
choice as pick,
|
|
94
113
|
percentage,
|
|
95
114
|
ifloat,
|
|
115
|
+
hex,
|
|
96
116
|
float,
|
|
97
117
|
src_default as default,
|
|
98
118
|
choice,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random hex string of the specified length.
|
|
3
|
+
* @param {number} [length=8] - The length of the hex string to generate. Default is `8`.
|
|
4
|
+
* @param {boolean} [prefix=false] - If true, the hex string will be prefixed with "0x". Default `false`.
|
|
5
|
+
* @returns {string} A random hex string of the specified length.
|
|
6
|
+
*/
|
|
7
|
+
export declare const hex: (length?: number, prefix?: boolean) => string;
|
package/dist/lib/shuffle.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Returns a new string containing the characters of the input string in a shuffled order.
|
|
3
3
|
*/
|
|
4
|
-
|
|
4
|
+
declare function shuffle(input: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Returns a new array containing the elements of the input array in a shuffled order.
|
|
7
|
+
*/
|
|
8
|
+
declare function shuffle<E>(input: E[]): E[];
|
|
9
|
+
export { shuffle };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "randomcryp",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"license": "BSD-3-Clause",
|
|
5
5
|
"description": "A cryptographically secure, feature rich, zero dependency and browser friendly random number generator library.",
|
|
6
6
|
"author": {
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"homepage": "https://github.com/touhidurrr/randomcryp",
|
|
12
12
|
"bugs": "https://github.com/touhidurrr/randomcryp/issues",
|
|
13
13
|
"funding": "https://buymeacoffee.com/touhidurrr",
|
|
14
|
+
"type": "module",
|
|
14
15
|
"main": "dist/index.js",
|
|
15
16
|
"module": "dist/index.js",
|
|
16
17
|
"types": "dist/index.d.ts",
|
|
@@ -19,7 +20,7 @@
|
|
|
19
20
|
"build": "bun build src/index.ts --outdir dist && tsc -p tsconfig.dts.json"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
|
-
"@types/bun": "^1.2.
|
|
23
|
+
"@types/bun": "^1.2.8",
|
|
23
24
|
"prettier": "^3.5.3",
|
|
24
25
|
"typescript": "^5.8.2"
|
|
25
26
|
},
|