@alwatr/random 5.1.19 โ†’ 5.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [5.1.21](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.20...@alwatr/random@5.1.21) (2025-10-06)
7
+
8
+ ### ๐Ÿ”— Dependencies update
9
+
10
+ * bump the npm-dependencies group with 4 updates ([9825815](https://github.com/Alwatr/nanolib/commit/982581552bbb4b97dca52af5e93a80937f0c3109))
11
+
12
+ ## [5.1.20](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.19...@alwatr/random@5.1.20) (2025-09-27)
13
+
14
+ ### ๐Ÿงน Miscellaneous Chores
15
+
16
+ * exclude test files from package distribution ([86f4f2f](https://github.com/Alwatr/nanolib/commit/86f4f2f5985845c5cf3a3a9398de7b2f98ce53e7))
17
+
6
18
  ## [5.1.19](https://github.com/Alwatr/nanolib/compare/@alwatr/random@5.1.18...@alwatr/random@5.1.19) (2025-09-22)
7
19
 
8
20
  **Note:** Version bump only for package @alwatr/random
package/dist/main.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /** ๐Ÿ“ฆ @alwatr/random v5.1.19 */
2
- __dev_mode__: console.debug("๐Ÿ“ฆ @alwatr/random v5.1.19");
1
+ /** ๐Ÿ“ฆ @alwatr/random v5.1.21 */
2
+ __dev_mode__: console.debug("๐Ÿ“ฆ @alwatr/random v5.1.21");
3
3
  "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});
4
4
  //# sourceMappingURL=main.cjs.map
package/dist/main.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /** ๐Ÿ“ฆ @alwatr/random v5.1.19 */
2
- __dev_mode__: console.debug("๐Ÿ“ฆ @alwatr/random v5.1.19");
1
+ /** ๐Ÿ“ฆ @alwatr/random v5.1.21 */
2
+ __dev_mode__: console.debug("๐Ÿ“ฆ @alwatr/random v5.1.21");
3
3
  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};
4
4
  //# sourceMappingURL=main.mjs.map
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@alwatr/random",
3
3
  "description": "A lightweight utility library for generating random numbers, strings, UUIDs and more",
4
- "version": "5.1.19",
4
+ "version": "5.1.21",
5
5
  "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
6
6
  "bugs": "https://github.com/Alwatr/nanolib/issues",
7
7
  "dependencies": {
8
- "@alwatr/global-this": "5.5.19"
8
+ "@alwatr/global-this": "5.6.1"
9
9
  },
10
10
  "devDependencies": {
11
- "@alwatr/nano-build": "6.3.3",
12
- "@alwatr/prettier-config": "5.0.4",
13
- "@alwatr/tsconfig-base": "6.0.2",
14
- "jest": "^30.1.3",
15
- "typescript": "^5.9.2"
11
+ "@alwatr/nano-build": "6.3.5",
12
+ "@alwatr/prettier-config": "5.0.5",
13
+ "@alwatr/tsconfig-base": "6.0.3",
14
+ "jest": "^30.2.0",
15
+ "typescript": "^5.9.3"
16
16
  },
17
17
  "exports": {
18
18
  ".": {
@@ -24,7 +24,8 @@
24
24
  "files": [
25
25
  "**/*.{js,mjs,cjs,map,d.ts,html,md,LEGAL.txt}",
26
26
  "LICENSE",
27
- "!demo/**/*"
27
+ "!demo/**/*",
28
+ "!**/*.test.js"
28
29
  ],
29
30
  "homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/random#readme",
30
31
  "keywords": [
@@ -88,5 +89,5 @@
88
89
  "sideEffects": false,
89
90
  "type": "module",
90
91
  "types": "./dist/main.d.ts",
91
- "gitHead": "a0c8605949969959c8226e54b8c8c0fc7276819a"
92
+ "gitHead": "b141732f4dab13542e3cc99926a250fd5c74bad3"
92
93
  }
package/src/main.test.js DELETED
@@ -1,377 +0,0 @@
1
- import {
2
- randNumber,
3
- randInteger,
4
- randFloat,
5
- randString,
6
- randStep,
7
- randShuffle,
8
- randPick,
9
- randBoolean,
10
- randColor,
11
- randUuid,
12
- randArray,
13
- bytesToHex,
14
- } from '@alwatr/random';
15
-
16
- describe('@alwatr/random', () => {
17
- describe('number generation', () => {
18
- test('randNumber() returns a number between 0 and 1', () => {
19
- const value = randNumber();
20
- expect(typeof value).toBe('number');
21
- expect(value).toBeGreaterThanOrEqual(0);
22
- expect(value).toBeLessThan(1);
23
- });
24
- });
25
-
26
- describe('integer generation', () => {
27
- test('randInteger() returns an integer within the specified range', () => {
28
- const min = 5;
29
- const max = 10;
30
- for (let i = 0; i < 100; i++) {
31
- const value = randInteger(min, max);
32
- expect(Number.isInteger(value)).toBe(true);
33
- expect(value).toBeGreaterThanOrEqual(min);
34
- expect(value).toBeLessThanOrEqual(max);
35
- }
36
- });
37
-
38
- test('randInteger() with min equal to max', () => {
39
- const value = randInteger(5, 5);
40
- expect(value).toBe(5);
41
- });
42
-
43
- test('randInteger() with negative numbers', () => {
44
- const min = -10;
45
- const max = -5;
46
- for (let i = 0; i < 100; i++) {
47
- const value = randInteger(min, max);
48
- expect(Number.isInteger(value)).toBe(true);
49
- expect(value).toBeGreaterThanOrEqual(min);
50
- expect(value).toBeLessThanOrEqual(max);
51
- }
52
- });
53
- });
54
-
55
- describe('float generation', () => {
56
- test('randFloat() returns a float within the specified range', () => {
57
- const min = 5;
58
- const max = 10;
59
- for (let i = 0; i < 100; i++) {
60
- const value = randFloat(min, max);
61
- expect(typeof value).toBe('number');
62
- expect(value).toBeGreaterThanOrEqual(min);
63
- expect(value).toBeLessThan(max);
64
- }
65
- });
66
-
67
- test('randFloat() with min equal to max', () => {
68
- const value = randFloat(5, 5);
69
- expect(value).toBe(5);
70
- });
71
-
72
- test('randFloat() with negative numbers', () => {
73
- const min = -10;
74
- const max = -5;
75
- for (let i = 0; i < 100; i++) {
76
- const value = randFloat(min, max);
77
- expect(typeof value).toBe('number');
78
- expect(value).toBeGreaterThanOrEqual(min);
79
- expect(value).toBeLessThan(max);
80
- }
81
- });
82
- });
83
-
84
- describe('string generation', () => {
85
- test('randString() with fixed length', () => {
86
- const length = 8;
87
- const str = randString(length);
88
- expect(str.length).toBe(length);
89
- expect(typeof str).toBe('string');
90
- });
91
-
92
- test('randString() with variable length', () => {
93
- const min = 5;
94
- const max = 10;
95
- const str = randString(min, max);
96
- expect(str.length).toBeGreaterThanOrEqual(min);
97
- expect(str.length).toBeLessThanOrEqual(max);
98
- });
99
-
100
- test('randString() with min equal to max', () => {
101
- const length = 5;
102
- const str = randString(length, length);
103
- expect(str.length).toBe(length);
104
- });
105
-
106
- test('randString() returns empty string when min is 0', () => {
107
- const str = randString(0);
108
- expect(str.length).toBe(0);
109
- });
110
-
111
- test('randString() with custom character set', () => {
112
- const length = 10;
113
- const chars = '01'; // Binary characters
114
- const str = randString(length, undefined, chars);
115
-
116
- expect(str.length).toBe(length);
117
- // Verify that only characters from the custom set are used
118
- expect(str.split('').every((char) => chars.includes(char))).toBe(true);
119
- });
120
-
121
- test('randString() with custom character set and variable length', () => {
122
- const min = 5;
123
- const max = 10;
124
- const chars = 'ABC123';
125
- const str = randString(min, max, chars);
126
-
127
- expect(str.length).toBeGreaterThanOrEqual(min);
128
- expect(str.length).toBeLessThanOrEqual(max);
129
- // Verify that only characters from the custom set are used
130
- expect(str.split('').every((char) => chars.includes(char))).toBe(true);
131
- });
132
-
133
- test('randString() optimizes for short strings', () => {
134
- // This test just ensures the code path for short strings works
135
- const str = randString(10);
136
- expect(str.length).toBe(10);
137
- });
138
-
139
- test('randString() handles longer strings with array join', () => {
140
- // This test ensures the code path for longer strings works
141
- const str = randString(20);
142
- expect(str.length).toBe(20);
143
- });
144
- });
145
-
146
- describe('step generation', () => {
147
- test('randStep() returns a value with the correct step', () => {
148
- const min = 0;
149
- const max = 10;
150
- const step = 2;
151
- const validValues = [0, 2, 4, 6, 8, 10];
152
-
153
- for (let i = 0; i < 100; i++) {
154
- const value = randStep(min, max, step);
155
- expect(validValues).toContain(value);
156
- }
157
- });
158
-
159
- test('randStep() with min equal to max', () => {
160
- const value = randStep(5, 5, 2);
161
- expect(value).toBe(5);
162
- });
163
-
164
- test('randStep() with step 0', () => {
165
- const value = randStep(5, 10, 0);
166
- expect(value).toBe(5);
167
- });
168
-
169
- test('randStep() with negative numbers', () => {
170
- const min = -10;
171
- const max = -2;
172
- const step = 2;
173
- const validValues = [-10, -8, -6, -4, -2];
174
-
175
- for (let i = 0; i < 100; i++) {
176
- const value = randStep(min, max, step);
177
- expect(validValues).toContain(value);
178
- }
179
- });
180
- });
181
-
182
- describe('array manipulation', () => {
183
- test('randShuffle() shuffles an array', () => {
184
- const original = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
185
- const shuffled = [...original];
186
- randShuffle(shuffled);
187
-
188
- // Test the arrays are not identical (with very high probability)
189
- // Note: there's a tiny chance this could fail even if randShuffle works correctly
190
- let isDifferent = false;
191
- for (let i = 0; i < original.length; i++) {
192
- if (original[i] !== shuffled[i]) {
193
- isDifferent = true;
194
- break;
195
- }
196
- }
197
- expect(isDifferent).toBe(true);
198
-
199
- // Make sure it contains all the same elements
200
- expect(original.sort()).toEqual(shuffled.sort());
201
- });
202
-
203
- test('randShuffle() does not modify an empty array', () => {
204
- const original = [];
205
- const shuffled = [...original];
206
- randShuffle(shuffled);
207
- expect(shuffled).toEqual(original);
208
- });
209
-
210
- test('randShuffle() returns the same array reference for chaining', () => {
211
- const array = [1, 2, 3, 4, 5];
212
- const result = randShuffle(array);
213
- expect(result).toBe(array); // Same reference
214
- });
215
-
216
- test('randPick() selects a valid array element', () => {
217
- const array = [1, 2, 3, 4, 5];
218
- for (let i = 0; i < 100; i++) {
219
- expect(array).toContain(randPick(array));
220
- }
221
- });
222
-
223
- test('randPick() throws on empty array', () => {
224
- expect(() => randPick([])).toThrow();
225
- });
226
- });
227
-
228
- describe('uuid generation', () => {
229
- test('randUuid() returns a valid UUID string', () => {
230
- const uuid = randUuid();
231
- const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
232
- expect(uuidRegex.test(uuid)).toBe(true);
233
- });
234
-
235
- test('randUuid() creates properly formatted UUID v4', () => {
236
- const uuid = randUuid();
237
- const parts = uuid.split('-');
238
-
239
- expect(parts.length).toBe(5);
240
- expect(parts[0].length).toBe(8);
241
- expect(parts[1].length).toBe(4);
242
- expect(parts[2].length).toBe(4);
243
- expect(parts[3].length).toBe(4);
244
- expect(parts[4].length).toBe(12);
245
-
246
- // Version 4 UUID has the 13th character as '4'
247
- expect(uuid.charAt(14)).toBe('4');
248
-
249
- // Variant should be binary 10xx (8, 9, a, or b)
250
- const variantChar = uuid.charAt(19);
251
- expect(['8', '9', 'a', 'b', 'A', 'B']).toContain(variantChar);
252
- });
253
- });
254
-
255
- describe('boolean generation', () => {
256
- test('randBoolean() returns a boolean', () => {
257
- expect(typeof randBoolean()).toBe('boolean');
258
- });
259
-
260
- test('randBoolean() with probability 0', () => {
261
- let trueCount = 0;
262
- const samples = 20;
263
-
264
- for (let i = 0; i < samples; i++) {
265
- if (randBoolean(0)) trueCount++;
266
- }
267
-
268
- expect(trueCount).toBe(0);
269
- });
270
-
271
- test('randBoolean() with probability 1', () => {
272
- let trueCount = 0;
273
- const samples = 20;
274
-
275
- for (let i = 0; i < samples; i++) {
276
- if (randBoolean(1)) trueCount++;
277
- }
278
-
279
- expect(trueCount).toBe(samples);
280
- });
281
-
282
- test('randBoolean() with default probability', () => {
283
- // This just checks that the function runs with default parameters
284
- // Statistical testing would be unreliable in unit tests
285
- const result = randBoolean();
286
- expect(typeof result).toBe('boolean');
287
- });
288
- });
289
-
290
- describe('color generation', () => {
291
- test('randColor() returns a valid hex color', () => {
292
- const color = randColor();
293
- const colorRegex = /^#[0-9a-f]{6}$/i;
294
- expect(colorRegex.test(color)).toBe(true);
295
- });
296
- });
297
-
298
- describe('random array filling', () => {
299
- test('randArray() fills typed array with random values', () => {
300
- const array = new Uint8Array(10);
301
- const emptyArray = new Uint8Array(10);
302
-
303
- randArray(array);
304
-
305
- // Extremely unlikely that a randomly filled array would equal an empty one
306
- expect(array).not.toEqual(emptyArray);
307
- });
308
-
309
- test('randArray() fills different typed arrays', () => {
310
- const array8 = new Uint8Array(10);
311
- const array16 = new Uint16Array(5);
312
- const array32 = new Uint32Array(2);
313
-
314
- randArray(array8);
315
- randArray(array16);
316
- randArray(array32);
317
-
318
- expect(array8.every(Number.isInteger)).toBe(true);
319
- expect(array16.every(Number.isInteger)).toBe(true);
320
- expect(array32.every(Number.isInteger)).toBe(true);
321
- });
322
-
323
- test('randArray() respects min and max parameters', () => {
324
- const array = new Uint8Array(100);
325
- const min = 10;
326
- const max = 20;
327
-
328
- randArray(array, min, max);
329
-
330
- // Check that all values are in range
331
- for (const value of array) {
332
- expect(value).toBeGreaterThanOrEqual(min);
333
- expect(value).toBeLessThanOrEqual(max);
334
- }
335
- });
336
-
337
- test('randArray() returns the same array for chaining', () => {
338
- const array = new Uint8Array(10);
339
- const result = randArray(array);
340
- expect(result).toBe(array); // Same reference
341
- });
342
-
343
- test('randArray() works with regular number arrays', () => {
344
- const array = new Array(5);
345
- randArray(array, -10, 10);
346
-
347
- expect(array.length).toBe(5);
348
- for (const value of array) {
349
- expect(typeof value).toBe('number');
350
- expect(value).toBeGreaterThanOrEqual(-10);
351
- expect(value).toBeLessThanOrEqual(10);
352
- }
353
- });
354
- });
355
-
356
- describe('bytesToHex', () => {
357
- test('converts Uint8Array to hex string', () => {
358
- const bytes = new Uint8Array([10, 255, 0, 16]);
359
- expect(bytesToHex(bytes)).toBe('0aff0010');
360
- });
361
-
362
- test('converts number array to hex string', () => {
363
- const array = [171, 205, 3];
364
- expect(bytesToHex(array)).toBe('abcd03');
365
- });
366
-
367
- test('handles empty array', () => {
368
- expect(bytesToHex([])).toBe('');
369
- expect(bytesToHex(new Uint8Array(0))).toBe('');
370
- });
371
-
372
- test('pads single-digit hex values with leading zero', () => {
373
- const bytes = new Uint8Array([0, 1, 2, 3, 10, 15]);
374
- expect(bytesToHex(bytes)).toBe('000102030a0f');
375
- });
376
- });
377
- });