Skip to content

Commit

Permalink
fix: remove v4 options default assignment preventing native.randomUUI…
Browse files Browse the repository at this point in the history
…D 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
  • Loading branch information
molaeiali committed Aug 7, 2024
1 parent 18f9493 commit afe6232
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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 @@ describe('v4', () => {
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();
});

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();
});

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

0 comments on commit afe6232

Please sign in to comment.