randomcryp 1.0.5 → 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 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 ~3KB in size.
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 | 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
- | **`choice(arr: ArrayLike<E>): E`** | Selects a random element from an array. | `choice([1, 2, 3, 4, 5])` → `3` |
52
- | **`pick(arr: ArrayLike<E>): E`** | Alias for `choice()`. | `pick([1, 2, 3, 4, 5])` → `1` |
53
- | **`shuffle(arr: Array<E>): E`** | Shuffles the elements of an array randomly in-place and returns a reference to the original array. | `shuffle([1, 2, 3, 4, 5])` → `[ 1, 3, 2, 5, 4 ]` |
54
- | **`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` |
55
- | **`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` |
56
- | **`randInt(min: number, max: number): number`** | Alias for `rangeInt()`. | `randInt(1, 100)` → `35` |
57
- | **`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` |
58
- | **`ifloat(): number`** | Generates a random number between `-1` (inclusive) and `1` (inclusive). Uses `safeInt()` and thus not recommended. | `ifloat()` → `-0.6076475248861822` |
59
-
60
- © 2025, Md. Touhidur Rahman
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
@@ -1,129 +1,34 @@
1
- declare module "lib/uSafeInt" {
2
- /**
3
- * Generates a random integer
4
- * between 0 (inclusive) and {@link Number.MAX_SAFE_INTEGER} (inclusive)
5
- * with 53 bits precission.
6
- **/
7
- export const uSafeInt: () => number;
8
- }
9
- declare module "lib/bool" {
10
- /**
11
- * Generates a boolean value randomly.
12
- * true and false are equally likely.
13
- **/
14
- export const bool: () => boolean;
15
- }
16
- declare module "lib/boolean" {
17
- export { bool as boolean } from "lib/bool";
18
- }
19
- declare module "lib/float" {
20
- /**
21
- * Generates a random number between 0 (inclusive) and 1 (exclusive).
22
- * with 53 bits precission.
23
- **/
24
- export const float: () => number;
25
- }
26
- declare module "lib/choice" {
27
- /**
28
- * Randomly choose an element from an array-like object.
29
- * @returns undefined if the array is empty.
30
- * @returns a random element from the array if at least one element is present.
31
- * @example choice([1, 2, 3]) // 2
32
- */
33
- export const choice: <E>(choices: ArrayLike<E>) => E | undefined;
34
- }
35
- declare module "lib/safeInt" {
36
- /**
37
- * Generates a random integer
38
- * between {@link Number.MIN_SAFE_INTEGER} (inclusive) and {@link Number.MAX_SAFE_INTEGER} (inclusive).
39
- * +0 and -0 can both appear, and technically 54 bit in precision but not recommended for general usage.
40
- *
41
- * Use {@link uSafeInt} for instead.
42
- **/
43
- export const safeInt: () => number;
44
- }
45
- declare module "lib/ifloat" {
46
- /**
47
- * Generates a random number between -1 (inclusive) and 1 (inclusive).
48
- * Uses {@link safeInt} and thus not recommended for general usage also.
49
- *
50
- * Should be somewhat better for calculating PI.
51
- **/
52
- export const ifloat: () => number;
53
- }
54
- declare module "lib/percentage" {
55
- /**
56
- * Generates true with the probability of the percentage value given.
57
- * @example percentage(20) // should return true 20% of the time and false 80% of the time.
58
- **/
59
- export const percentage: (input: number) => boolean;
60
- }
61
- declare module "lib/pick" {
62
- export { choice as pick } from "lib/choice";
63
- }
64
- declare module "lib/rangeInt" {
65
- /**
66
- * Generates a random number integer
67
- * between given `min` (inclusive) and `max` (inclusive).
68
- * Throws if `min` > `max`.
69
- *
70
- * @example rangeInt(1, 100) // 35
71
- */
72
- export const rangeInt: (min: number, max: number) => number;
73
- }
74
- declare module "lib/randInt" {
75
- export { rangeInt as randInt } from "lib/rangeInt";
76
- }
77
- declare module "lib/random" {
78
- export { float as random } from "lib/float";
79
- }
80
- declare module "lib/range" {
81
- /**
82
- * Generates a random number (not integer)
83
- * between given `min` (inclusive) and `max` (exclusive).
84
- * Throws if `min` > `max`.
85
- *
86
- * @example range(1, 10) // 2.4802525465053487
87
- */
88
- export const range: (min: number, max: number) => number;
89
- }
90
- declare module "lib/shuffle" {
91
- /**
92
- * Randomly shuffles the elements of an array ***in place***. The original array is modified.
93
- */
94
- export const shuffle: <E>(array: Array<E>) => Array<E>;
95
- }
96
- declare module "index" {
97
- import { bool } from "lib/bool";
98
- import { boolean } from "lib/boolean";
99
- import { choice } from "lib/choice";
100
- import { float } from "lib/float";
101
- import { ifloat } from "lib/ifloat";
102
- import { percentage } from "lib/percentage";
103
- import { pick } from "lib/pick";
104
- import { randInt } from "lib/randInt";
105
- import { random } from "lib/random";
106
- import { range } from "lib/range";
107
- import { rangeInt } from "lib/rangeInt";
108
- import { safeInt } from "lib/safeInt";
109
- import { shuffle } from "lib/shuffle";
110
- import { uSafeInt } from "lib/uSafeInt";
111
- export { bool, boolean, choice, float, ifloat, percentage, pick, randInt, random, range, rangeInt, safeInt, shuffle, uSafeInt, };
112
- const _default: {
113
- bool: () => boolean;
114
- boolean: () => boolean;
115
- choice: <E>(choices: ArrayLike<E>) => E | undefined;
116
- float: () => number;
117
- ifloat: () => number;
118
- percentage: (input: number) => boolean;
119
- pick: <E>(choices: ArrayLike<E>) => E | undefined;
120
- randInt: (min: number, max: number) => number;
121
- random: () => number;
122
- range: (min: number, max: number) => number;
123
- rangeInt: (min: number, max: number) => number;
124
- safeInt: () => number;
125
- shuffle: <E>(array: Array<E>) => Array<E>;
126
- uSafeInt: () => number;
127
- };
128
- export default _default;
129
- }
1
+ import { bool } from "./lib/bool";
2
+ import { boolean } from "./lib/boolean";
3
+ import { choice } from "./lib/choice";
4
+ import { float } from "./lib/float";
5
+ import { hex } from "./lib/hex";
6
+ import { ifloat } from "./lib/ifloat";
7
+ import { percentage } from "./lib/percentage";
8
+ import { pick } from "./lib/pick";
9
+ import { randInt } from "./lib/randInt";
10
+ import { random } from "./lib/random";
11
+ import { range } from "./lib/range";
12
+ import { rangeInt } from "./lib/rangeInt";
13
+ import { safeInt } from "./lib/safeInt";
14
+ import { shuffle } from "./lib/shuffle";
15
+ import { uSafeInt } from "./lib/uSafeInt";
16
+ export { bool, boolean, choice, float, hex, ifloat, percentage, pick, randInt, random, range, rangeInt, safeInt, shuffle, uSafeInt, };
17
+ declare const _default: {
18
+ bool: () => boolean;
19
+ boolean: () => boolean;
20
+ choice: <E>(choices: ArrayLike<E>) => E | undefined;
21
+ float: () => number;
22
+ hex: (length?: number, prefix?: boolean) => string;
23
+ ifloat: () => number;
24
+ percentage: (input: number) => boolean;
25
+ pick: <E>(choices: ArrayLike<E>) => E | undefined;
26
+ randInt: (min: number, max: number) => number;
27
+ random: () => number;
28
+ range: (min: number, max: number) => number;
29
+ rangeInt: (min: number, max: number) => number;
30
+ safeInt: () => number;
31
+ shuffle: typeof shuffle;
32
+ uSafeInt: () => number;
33
+ };
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
- var shuffle = (array) => {
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,5 @@
1
+ /**
2
+ * Generates a boolean value randomly.
3
+ * true and false are equally likely.
4
+ **/
5
+ export declare const bool: () => boolean;
@@ -0,0 +1 @@
1
+ export { bool as boolean } from "./bool";
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Randomly choose an element from an array-like object.
3
+ * @returns undefined if the array is empty.
4
+ * @returns a random element from the array if at least one element is present.
5
+ * @example choice([1, 2, 3]) // 2
6
+ */
7
+ export declare const choice: <E>(choices: ArrayLike<E>) => E | undefined;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generates a random number between 0 (inclusive) and 1 (exclusive).
3
+ * with 53 bits precission.
4
+ **/
5
+ export declare const float: () => number;
@@ -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;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Generates a random number between -1 (inclusive) and 1 (inclusive).
3
+ * Uses {@link safeInt} and thus not recommended for general usage also.
4
+ *
5
+ * Should be somewhat better for calculating PI.
6
+ **/
7
+ export declare const ifloat: () => number;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generates true with the probability of the percentage value given.
3
+ * @example percentage(20) // should return true 20% of the time and false 80% of the time.
4
+ **/
5
+ export declare const percentage: (input: number) => boolean;
@@ -0,0 +1 @@
1
+ export { choice as pick } from "./choice";
@@ -0,0 +1 @@
1
+ export { rangeInt as randInt } from "./rangeInt";
@@ -0,0 +1 @@
1
+ export { float as random } from "./float";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Generates a random number (not integer)
3
+ * between given `min` (inclusive) and `max` (exclusive).
4
+ * Throws if `min` > `max`.
5
+ *
6
+ * @example range(1, 10) // 2.4802525465053487
7
+ */
8
+ export declare const range: (min: number, max: number) => number;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Generates a random number integer
3
+ * between given `min` (inclusive) and `max` (inclusive).
4
+ * Throws if `min` > `max`.
5
+ *
6
+ * @example rangeInt(1, 100) // 35
7
+ */
8
+ export declare const rangeInt: (min: number, max: number) => number;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Generates a random integer
3
+ * between {@link Number.MIN_SAFE_INTEGER} (inclusive) and {@link Number.MAX_SAFE_INTEGER} (inclusive).
4
+ * +0 and -0 can both appear, and technically 54 bit in precision but not recommended for general usage.
5
+ *
6
+ * Use {@link uSafeInt} for instead.
7
+ **/
8
+ export declare const safeInt: () => number;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Returns a new string containing the characters of the input string in a shuffled order.
3
+ */
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 };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Generates a random integer
3
+ * between 0 (inclusive) and {@link Number.MAX_SAFE_INTEGER} (inclusive)
4
+ * with 53 bits precission.
5
+ **/
6
+ export declare const uSafeInt: () => number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "randomcryp",
3
- "version": "1.0.5",
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.5",
23
+ "@types/bun": "^1.2.8",
23
24
  "prettier": "^3.5.3",
24
25
  "typescript": "^5.8.2"
25
26
  },