Skip to content

Commit

Permalink
Improves test() function to pass for uuid versions 1-5
Browse files Browse the repository at this point in the history
  • Loading branch information
jchook committed Jun 24, 2020
1 parent 06ca8aa commit f76abc4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// Test for uuid
uuid.test = function(uuid) {
if (typeof uuid === 'string') {
return /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid);
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid);
}
return false;
};
Expand Down
31 changes: 22 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@ var uuid = require('./index');

// Check format
var i;
for (i = 0; i<10000; i++) {
assert(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(uuid()));
for (i = 0; i < 10000; i++) {
assert(uuid.test(uuid()));
}

// Check the test function on known-valid UUIDs
const healthyUuid = '920b70bf-168a-458d-81ac-50e488e5976f'
assert(uuid.test(healthyUuid) === true)
assert(uuid.test(healthyUuid.toUpperCase()) === true)
// Check the test function on known-[in]valid UUIDs
const healthyUuids = ['6514db12-0a68-4108-a8c9-3ddc6f489a26'];
const invalidUuids = [
'920b70bf-168a-458d-c1ac-50e488e5976f',
'633ca20d-1e9e-7b81-b725-82a595ce3515',
'knuth',
undefined,
null,
[],
42,
];
for (const healthyUuid of healthyUuids) {
assert(uuid.test(healthyUuid) === true);
assert(uuid.test(healthyUuid.toUpperCase()) === true);
}
for (const invalidUuid of invalidUuids) {
assert(uuid.test(invalidUuid) === false);
}

// Clear the buffer and change the randomBytes function to return 0s
uuid.clearBuffer();
uuid.randomBytes = function(length) {
return (new Array(length)).fill(0, 0, length);
uuid.randomBytes = function (length) {
return new Array(length).fill(0, 0, length);
};
assert(uuid() === '00000000-0000-4000-8000-000000000000');

0 comments on commit f76abc4

Please sign in to comment.