Skip to content

Commit

Permalink
fix: handle error when parameter is not set in v3 and v5 (#622)
Browse files Browse the repository at this point in the history
* fix: unhandle error when parameter is not set in v3 and v5

* test: add unit test for v3 and v5 to test undefined/null `namespace`

Authored-by: rzkytmgr <rzkytmgr@gmail.com>
  • Loading branch information
ctavan authored Mar 17, 2022
1 parent 4f99b5e commit fcd7388
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/v35.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function v35(name, version, hashfunc) {
namespace = parse(namespace);
}

if (namespace.length !== 16) {
if (namespace?.length !== 16) {
throw TypeError('Namespace must be array-like (16 iterable integer values, 0-255)');
}

Expand Down
36 changes: 36 additions & 0 deletions test/unit/v35.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ describe('v35', () => {
assert.deepEqual(buf, ['landmaster', 'landmaster', 'landmaster'].concat(testBuf));
});

test('v3 undefined/null', () => {
assert.throws(() => {
v3();
});

assert.throws(() => {
v3('hello');
});

assert.throws(() => {
v3('hello.example.com', undefined);
});

assert.throws(() => {
v3('hello.example.com', null, new Array(16));
});
});

test('v5', () => {
// Expect to get the same results as http://tools.adjet.org/uuid-v5
assert.strictEqual(v5('hello.example.com', v5.DNS), 'fdda765f-fc57-5604-a269-52a7df8164ec');
Expand Down Expand Up @@ -229,6 +247,24 @@ describe('v35', () => {
assert.deepEqual(buf, ['landmaster', 'landmaster', 'landmaster'].concat(testBuf));
});

test('v5 undefined/null', () => {
assert.throws(() => {
v5();
});

assert.throws(() => {
v5('hello');
});

assert.throws(() => {
v5('hello.example.com', undefined);
});

assert.throws(() => {
v5('hello.example.com', null, new Array(16));
});
});

test('v3/v5 constants', () => {
assert.strictEqual(v3.DNS, '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
assert.strictEqual(v3.URL, '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
Expand Down

0 comments on commit fcd7388

Please sign in to comment.