Skip to content

Commit afe6232

Browse files
authoredAug 7, 2024··
fix: remove v4 options default assignment preventing native.randomUUID from being used (#786)
* fix: remove options default assignment introduced during porting to ts, pull #763 preventing native.randomUUID being used * test: add unit test to check if native randomUUID should be used or not
1 parent 18f9493 commit afe6232

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed
 

‎src/test/v4.test.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as assert from 'assert';
2-
import test, { describe } from 'node:test';
2+
import test, { describe, mock } from 'node:test';
33
import v4 from '../v4.js';
4+
import native from '../native.js';
45

56
const randomBytesFixture = Uint8Array.of(
67
0x10,
@@ -48,6 +49,26 @@ describe('v4', () => {
4849
assert.ok(id1 !== id2);
4950
});
5051

52+
test('should uses native randomUUID() if no option is passed', () => {
53+
mock.method(native, 'randomUUID');
54+
55+
assert.equal((native.randomUUID as any).mock.callCount(), 0);
56+
v4();
57+
assert.equal((native.randomUUID as any).mock.callCount(), 1);
58+
59+
mock.restoreAll();
60+
});
61+
62+
test('should not use native randomUUID() if an option is passed', () => {
63+
mock.method(native, 'randomUUID');
64+
65+
assert.equal((native.randomUUID as any).mock.callCount(), 0);
66+
v4({});
67+
assert.equal((native.randomUUID as any).mock.callCount(), 0);
68+
69+
mock.restoreAll();
70+
});
71+
5172
test('explicit options.random produces expected result', () => {
5273
const id = v4({ random: randomBytesFixture });
5374
assert.strictEqual(id, '109156be-c4fb-41ea-b1b4-efe1671c5836');

‎src/v4.ts

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { unsafeStringify } from './stringify.js';
66
function v4(options?: Version4Options, buf?: undefined, offset?: number): string;
77
function v4(options?: Version4Options, buf?: Uint8Array, offset?: number): Uint8Array;
88
function v4(options?: Version4Options, buf?: Uint8Array, offset?: number): UUIDTypes {
9-
options ??= {};
10-
119
if (native.randomUUID && !buf && !options) {
1210
return native.randomUUID();
1311
}

0 commit comments

Comments
 (0)
Please sign in to comment.