Skip to content

Commit fcd7388

Browse files
authoredMar 17, 2022
fix: handle error when parameter is not set in v3 and v5 (#622)
* 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>
1 parent 4f99b5e commit fcd7388

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
 

‎src/v35.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function v35(name, version, hashfunc) {
2626
namespace = parse(namespace);
2727
}
2828

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

‎test/unit/v35.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,24 @@ describe('v35', () => {
148148
assert.deepEqual(buf, ['landmaster', 'landmaster', 'landmaster'].concat(testBuf));
149149
});
150150

151+
test('v3 undefined/null', () => {
152+
assert.throws(() => {
153+
v3();
154+
});
155+
156+
assert.throws(() => {
157+
v3('hello');
158+
});
159+
160+
assert.throws(() => {
161+
v3('hello.example.com', undefined);
162+
});
163+
164+
assert.throws(() => {
165+
v3('hello.example.com', null, new Array(16));
166+
});
167+
});
168+
151169
test('v5', () => {
152170
// Expect to get the same results as http://tools.adjet.org/uuid-v5
153171
assert.strictEqual(v5('hello.example.com', v5.DNS), 'fdda765f-fc57-5604-a269-52a7df8164ec');
@@ -229,6 +247,24 @@ describe('v35', () => {
229247
assert.deepEqual(buf, ['landmaster', 'landmaster', 'landmaster'].concat(testBuf));
230248
});
231249

250+
test('v5 undefined/null', () => {
251+
assert.throws(() => {
252+
v5();
253+
});
254+
255+
assert.throws(() => {
256+
v5('hello');
257+
});
258+
259+
assert.throws(() => {
260+
v5('hello.example.com', undefined);
261+
});
262+
263+
assert.throws(() => {
264+
v5('hello.example.com', null, new Array(16));
265+
});
266+
});
267+
232268
test('v3/v5 constants', () => {
233269
assert.strictEqual(v3.DNS, '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
234270
assert.strictEqual(v3.URL, '6ba7b811-9dad-11d1-80b4-00c04fd430c8');

0 commit comments

Comments
 (0)
Please sign in to comment.