-
-
Notifications
You must be signed in to change notification settings - Fork 911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: parsing non RFC uuid values #455
Changes from 11 commits
214533c
fbc317e
57ab10a
fc6cc23
865e6e7
3fd422b
d6851cb
10e1bfa
b57dc67
f52d4e8
d096cc2
70f4179
2218267
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const UUID_REGEXP = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i; | ||
|
||
export default UUID_REGEXP; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import bytesToUuid from './bytesToUuid.js'; | ||
import validate from './validate.js'; | ||
|
||
function uuidToBytes(uuid) { | ||
// Note: We assume we're being passed a valid uuid string | ||
const bytes = []; | ||
// Char offset to hex pairs in uuid strings | ||
const HEX_PAIRS = [0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34]; | ||
|
||
uuid.replace(/[a-fA-F0-9]{2}/g, function (hex) { | ||
bytes.push(parseInt(hex, 16)); | ||
}); | ||
function uuidToBytes(uuid) { | ||
if (!validate(uuid)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simpler, more compact, more performant(???) implementation: // Char offset to hex pairs in uuid strings
const HEX_PAIRS=[0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34];
function uuidToBytes(uuid) {
if (!validate(uuid)) throw TypeError('Invalid UUID');
return HEX_PAIRS.map(i => parseInt(uuid.slice(i, i + 2), 16));
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @broofa I checked this code performance:
This code is shorter, but less productive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would again favor conciseness over performance in this case. |
||
throw TypeError('Invalid UUID'); | ||
} | ||
|
||
return bytes; | ||
return HEX_PAIRS.map((i) => parseInt(uuid.substr(i, 2), 16)); | ||
} | ||
|
||
function stringToBytes(str) { | ||
|
@@ -28,10 +29,13 @@ export const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; | |
|
||
export default function (name, version, hashfunc) { | ||
function generateUUID(value, namespace, buf, offset) { | ||
const off = (buf && offset) || 0; | ||
if (typeof value === 'string') { | ||
value = stringToBytes(value); | ||
} | ||
|
||
if (typeof value === 'string') value = stringToBytes(value); | ||
if (typeof namespace === 'string') namespace = uuidToBytes(namespace); | ||
if (typeof namespace === 'string') { | ||
namespace = uuidToBytes(namespace); | ||
} | ||
|
||
if (!Array.isArray(value)) { | ||
throw TypeError('value must be an array of bytes'); | ||
|
@@ -47,12 +51,16 @@ export default function (name, version, hashfunc) { | |
bytes[8] = (bytes[8] & 0x3f) | 0x80; | ||
|
||
if (buf) { | ||
for (let idx = 0; idx < 16; ++idx) { | ||
buf[off + idx] = bytes[idx]; | ||
offset = offset || 0; | ||
|
||
for (let i = 0; i < 16; ++i) { | ||
buf[offset + i] = bytes[i]; | ||
} | ||
|
||
return buf; | ||
} | ||
|
||
return buf || bytesToUuid(bytes); | ||
return bytesToUuid(bytes); | ||
} | ||
|
||
// Function#name is not settable on some platforms (#270) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import REGEX from './regex.js'; | ||
|
||
function validate(uuid) { | ||
return typeof uuid === 'string' && REGEX.test(uuid); | ||
} | ||
|
||
export default validate; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import validate from './validate.js'; | ||
|
||
function version(uuid) { | ||
if (!validate(uuid)) { | ||
throw TypeError('Invalid UUID'); | ||
} | ||
|
||
return parseInt(uuid.substr(14, 1), 16); | ||
} | ||
|
||
export default version; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import assert from 'assert'; | ||
import validate from '../../src/validate.js'; | ||
|
||
describe('validate', () => { | ||
test('validate uuid', () => { | ||
assert.strictEqual(validate('00000000-0000-0000-0000-000000000000'), true); | ||
broofa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert.strictEqual(validate('d9428888-122b-11e1-b85c-61cd3cbb3210'), true); | ||
|
||
assert.strictEqual(validate('109156be-c4fb-41ea-b1b4-efe1671c5836'), true); | ||
|
||
assert.strictEqual(validate('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'), true); | ||
|
||
assert.strictEqual(validate('90123e1c-7512-523e-bb28-76fab9f2f73d'), true); | ||
|
||
assert.strictEqual(validate(), false); | ||
|
||
assert.strictEqual(validate(''), false); | ||
|
||
assert.strictEqual(validate('invalid uuid string'), false); | ||
|
||
assert.strictEqual(validate('00000000000000000000000000000000'), false); | ||
broofa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert.strictEqual( | ||
validate( | ||
'=Y00a-f*v00b*-00c-00d#-p00f\b-00g-00h-####00i^^^-00j*1*2*3&-L00k-\n00l-/00m-----00n-fg000-00p-00r+', | ||
), | ||
false, | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import assert from 'assert'; | ||
import version from '../../src/version.js'; | ||
|
||
describe('version', () => { | ||
test('check uuid version', () => { | ||
assert.strictEqual(version('00000000-0000-0000-0000-000000000000'), 0); | ||
|
||
assert.strictEqual(version('d9428888-122b-11e1-b85c-61cd3cbb3210'), 1); | ||
|
||
assert.strictEqual(version('109156be-c4fb-41ea-b1b4-efe1671c5836'), 4); | ||
|
||
assert.strictEqual(version('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'), 3); | ||
|
||
assert.strictEqual(version('90123e1c-7512-523e-bb28-76fab9f2f73d'), 5); | ||
|
||
assert.throws(() => version()); | ||
|
||
assert.throws(() => version('')); | ||
|
||
assert.throws(() => version('invalid uuid string')); | ||
|
||
assert.throws(() => { | ||
version('00000000000000000000000000000000'); | ||
}); | ||
|
||
assert.throws(() => { | ||
version( | ||
'=Y00a-f*v00b*-00c-00d#-p00f\b-00g-00h-####00i^^^-00j*1*2*3&-L00k-\n00l-/00m-----00n-fg000-00p-00r+', | ||
); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked again and I think this is not necessary:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ctavan is explicit better than implicit? =)