Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove v4 options default assignment preventing native.randomUUID from being used #786

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/test/v4.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import test, { describe, mock } from 'node:test';
import v4 from '../v4.js';
import native from '../native.js';

const randomBytesFixture = Uint8Array.of(
0x10,
Expand Down Expand Up @@ -48,6 +49,26 @@
assert.ok(id1 !== id2);
});

test('should uses native randomUUID() if no option is passed', () => {
mock.method(native, 'randomUUID');

assert.equal((native.randomUUID as any).mock.callCount(), 0);

Check failure on line 55 in src/test/v4.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
v4();
assert.equal((native.randomUUID as any).mock.callCount(), 1);

Check failure on line 57 in src/test/v4.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

mock.restoreAll();
broofa marked this conversation as resolved.
Show resolved Hide resolved
});

test('should not use native randomUUID() if an option is passed', () => {
mock.method(native, 'randomUUID');

assert.equal((native.randomUUID as any).mock.callCount(), 0);

Check failure on line 65 in src/test/v4.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
v4({});
assert.equal((native.randomUUID as any).mock.callCount(), 0);

Check failure on line 67 in src/test/v4.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

mock.restoreAll();
broofa marked this conversation as resolved.
Show resolved Hide resolved
});

test('explicit options.random produces expected result', () => {
const id = v4({ random: randomBytesFixture });
assert.strictEqual(id, '109156be-c4fb-41ea-b1b4-efe1671c5836');
Expand Down
2 changes: 0 additions & 2 deletions src/v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { unsafeStringify } from './stringify.js';
function v4(options?: Version4Options, buf?: undefined, offset?: number): string;
function v4(options?: Version4Options, buf?: Uint8Array, offset?: number): Uint8Array;
function v4(options?: Version4Options, buf?: Uint8Array, offset?: number): UUIDTypes {
options ??= {};

if (native.randomUUID && !buf && !options) {
return native.randomUUID();
}
Expand Down
Loading