@vpmedia/simplify 1.70.0 → 1.71.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/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## [1.71.0] - 2026-02-05
2
+
3
+ ### 🚜 Refactor
4
+
5
+ - Improve uuidv4 generator
6
+
7
+ ### 🧪 Testing
8
+
9
+ - Adjusted uuid test
10
+ - Improve uuid tests
11
+
12
+ ### ⚙️ Miscellaneous Tasks
13
+
14
+ - Release
15
+ - Regenerate types and source maps
16
+ - *(release)* V1.71.0
1
17
  ## [1.70.0] - 2026-02-04
2
18
 
3
19
  ### 🚀 Features
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vpmedia/simplify",
3
- "version": "1.70.0",
3
+ "version": "1.71.0",
4
4
  "description": "@vpmedia/simplify",
5
5
  "author": "Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)",
6
6
  "license": "MIT",
package/src/util/uuid.js CHANGED
@@ -1,19 +1,37 @@
1
1
  /* eslint-disable no-bitwise, unicorn/number-literal-case */
2
2
 
3
- export const hex = (s, b) => s + (b >>> 4).toString(16) + (b & 0b1111).toString(16);
3
+ /**
4
+ * Convert a byte (0–255) to a 2‑character hex string.
5
+ * @param {number} byte - Byte value.
6
+ * @returns {string} Hex value.
7
+ */
8
+ export const byteToHex = (byte) => (byte >>> 4).toString(16) + (byte & 0b1111).toString(16);
4
9
 
10
+ /**
11
+ * UUIDv4 fallback generator (RFC 4122 compliant).
12
+ * @returns {string} UUIDv4 string.
13
+ */
5
14
  export const randomUUIDFallback = () => {
6
- const r = crypto.getRandomValues
15
+ const bytes = crypto.getRandomValues
7
16
  ? crypto.getRandomValues(new Uint8Array(16))
8
17
  : Array.from({ length: 16 }, () => Math.floor(Math.random() * 256));
9
- r[6] = (r[6] & 0x0f) | 0x40;
10
- r[8] = (r[8] & 0x3f) | 0x80;
11
- return [...r].reduce((uuid, b, i) => uuid + hex(i === 4 || i === 6 || i === 8 || i === 10 ? '-' : '', b), '');
12
- };
13
18
 
14
- if (!crypto.randomUUID) {
15
- // @ts-expect-error
16
- crypto.randomUUID = randomUUIDFallback;
17
- }
19
+ // RFC 4122 version & variant bits
20
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
21
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
22
+
23
+ let uuid = '';
24
+ for (const [index, byte] of bytes.entries()) {
25
+ if (index === 4 || index === 6 || index === 8 || index === 10) {
26
+ uuid += '-';
27
+ }
28
+ uuid += byteToHex(byte);
29
+ }
30
+ return uuid;
31
+ };
18
32
 
19
- export const uuidv4 = () => crypto.randomUUID();
33
+ /**
34
+ * Crypto UUIDv4 wrapper with fallback.
35
+ * @returns {string} UUIDv4 string.
36
+ */
37
+ export const uuidv4 = () => (typeof crypto.randomUUID === 'function' ? crypto.randomUUID() : randomUUIDFallback());
@@ -33,7 +33,21 @@ describe('UUID functions', () => {
33
33
  });
34
34
 
35
35
  it('randomUUIDFallback fallback works if crypto.randomUUID not available', () => {
36
+ delete crypto.randomUUID;
36
37
  const uuid = randomUUIDFallback();
37
38
  expect(uuid).toMatch(uuidV4Regex);
38
39
  });
40
+
41
+ it('randomUUIDFallback does not generate duplicates', () => {
42
+ const uuidMap = new Map();
43
+ const numSamples = 1000;
44
+ for (let i = 0; i < numSamples; i += 1) {
45
+ const uuid = randomUUIDFallback();
46
+ if (uuidMap.has(uuid)) {
47
+ throw new Error('Duplicate UUIDv4 found');
48
+ }
49
+ uuidMap.set(uuid, true);
50
+ }
51
+ expect(uuidMap.size).toBe(numSamples);
52
+ });
39
53
  });
@@ -1,4 +1,4 @@
1
- export function hex(s: any, b: any): string;
1
+ export function byteToHex(byte: number): string;
2
2
  export function randomUUIDFallback(): string;
3
- export function uuidv4(): `${string}-${string}-${string}-${string}-${string}`;
3
+ export function uuidv4(): string;
4
4
  //# sourceMappingURL=uuid.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../../src/util/uuid.js"],"names":[],"mappings":"AAEO,4CAA4E;AAE5E,6CAON;AAOM,8EAAwC"}
1
+ {"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../../src/util/uuid.js"],"names":[],"mappings":"AAOO,gCAHI,MAAM,GACJ,MAAM,CAEwE;AAMpF,sCAFM,MAAM,CAmBlB;AAMM,0BAFM,MAAM,CAE+F"}