Skip to content

Commit

Permalink
Merge pull request #18 from jchook/17-case-insensitive-test
Browse files Browse the repository at this point in the history
Case-insensitive test()
  • Loading branch information
jchook authored Jun 24, 2020
2 parents 6e96a2c + 8d8a02f commit d191cb6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ time.
## 1.3.1 <small>- Jun 19, 2020</small>

- Fixes issue with iOS 9 ([#15](https://github.com/jchook/uuid-random/issues/15))
- Fixes issue with `test()` case-sensitivity ([#17](https://github.com/jchook/uuid-random/issues/17))
- Improves tests
- Improves benchmarks


Expand Down
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}$/.test(uuid);
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 false;
};
Expand Down
27 changes: 23 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +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-[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 d191cb6

Please sign in to comment.