Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d096cc2

Browse files
committedMay 29, 2020
feat: short version of parsing UUID
1 parent f52d4e8 commit d096cc2

11 files changed

+29
-45
lines changed
 

‎.babelrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
presets: [['@babel/preset-env', { targets: { node: '8' }, modules: 'commonjs' }]],
77
},
88
esmBrowser: {
9-
presets: [['@babel/preset-env', { modules: false }]],
9+
presets: [['@babel/preset-env', { targets: { ie: '11' }, modules: false }]],
1010
},
1111
esmNode: {
1212
presets: [['@babel/preset-env', { targets: { node: '8' }, modules: false }]],

‎.eslintrc.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
},
1414
"parser": "babel-eslint",
1515
"rules": {
16-
"no-var": ["error"],
17-
"curly": ["error", "all"]
16+
"no-var": ["error"]
1817
}
1918
}

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ ddeb27fb-d9a0-4624-be4d-4615062daed4
292292
The default is to generate version 4 UUIDS, however the other versions are supported. Type
293293
`uuid --help` for details:
294294

295-
```
295+
```shell
296296
$ uuid --help
297297

298298
Usage:
@@ -323,7 +323,7 @@ uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
323323

324324
To run the examples you must first create a dist build of this library in the module root:
325325

326-
```
326+
```shell
327327
npm run build
328328
```
329329

‎README_js.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ ddeb27fb-d9a0-4624-be4d-4615062daed4
281281
The default is to generate version 4 UUIDS, however the other versions are supported. Type
282282
`uuid --help` for details:
283283

284-
```
284+
```shell
285285
$ uuid --help
286286

287287
Usage:
@@ -312,7 +312,7 @@ uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
312312

313313
To run the examples you must first create a dist build of this library in the module root:
314314

315-
```
315+
```shell
316316
npm run build
317317
```
318318

‎src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export { default as v1 } from './v1.js';
22
export { default as v3 } from './v3.js';
33
export { default as v4 } from './v4.js';
44
export { default as v5 } from './v5.js';
5-
export { default as REGEX } from './regex.js';
6-
export { default as version } from './version.js';
7-
export { default as validate } from './validate.js';
5+
export { default as getVersionUUID } from './version.js';
6+
export { default as validateUUID } from './validate.js';
7+
export { default as UUID_REGEXP } from './regex.js';

‎src/regex.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const REGEX = /^(?:[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;
1+
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;
22

3-
export default REGEX;
3+
export default UUID_REGEXP;

‎src/uuid-bin.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@ switch (version) {
3939
assert(name != null, 'v3 name not specified');
4040
assert(namespace != null, 'v3 namespace not specified');
4141

42-
if (namespace === 'URL') {
43-
namespace = v3.URL;
44-
}
45-
46-
if (namespace === 'DNS') {
47-
namespace = v3.DNS;
48-
}
42+
if (namespace === 'URL') namespace = v3.URL;
43+
if (namespace === 'DNS') namespace = v3.DNS;
4944

5045
console.log(v3(name, namespace));
5146
break;
@@ -62,13 +57,8 @@ switch (version) {
6257
assert(name != null, 'v5 name not specified');
6358
assert(namespace != null, 'v5 namespace not specified');
6459

65-
if (namespace === 'URL') {
66-
namespace = v5.URL;
67-
}
68-
69-
if (namespace === 'DNS') {
70-
namespace = v5.DNS;
71-
}
60+
if (namespace === 'URL') namespace = v5.URL;
61+
if (namespace === 'DNS') namespace = v5.DNS;
7262

7363
console.log(v5(name, namespace));
7464
break;

‎src/v35.js

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
import bytesToUuid from './bytesToUuid.js';
22
import validate from './validate.js';
33

4-
// Int32 to 4 bytes https://stackoverflow.com/a/12965194/3684944
5-
function numberToBytes(num, bytes, offset) {
6-
for (let i = 0; i < 4; ++i) {
7-
const byte = num & 0xff;
8-
// Fill the 4 bytes right-to-left.
9-
bytes[offset + 3 - i] = byte;
10-
num = (num - byte) / 256;
11-
}
12-
}
4+
// Char offset to hex pairs in uuid strings
5+
const HEX_PAIRS = [0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34];
136

147
function uuidToBytes(uuid) {
158
if (!validate(uuid)) {
16-
return [];
9+
throw TypeError('Invalid UUID');
1710
}
1811

19-
const bytes = new Array(16);
20-
21-
numberToBytes(parseInt(uuid.slice(0, 8), 16), bytes, 0);
22-
numberToBytes(parseInt(uuid.slice(9, 13) + uuid.slice(14, 18), 16), bytes, 4);
23-
numberToBytes(parseInt(uuid.slice(19, 23) + uuid.slice(24, 28), 16), bytes, 8);
24-
numberToBytes(parseInt(uuid.slice(28), 16), bytes, 12);
25-
26-
return bytes;
12+
return HEX_PAIRS.map((i) => parseInt(uuid.substr(i, 2), 16));
2713
}
2814

2915
function stringToBytes(str) {

‎test/unit/v35.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import sha1Browser from '../../src/sha1-browser.js';
66
import v3 from '../../src/v3.js';
77
import v5 from '../../src/v5.js';
88

9-
describe('v5', () => {
9+
describe('v35', () => {
1010
const HASH_SAMPLES = [
1111
{
1212
input: '',
@@ -35,6 +35,7 @@ describe('v5', () => {
3535
if (hash instanceof Buffer) {
3636
hash = Array.from(hash);
3737
}
38+
3839
return hash
3940
.map(function (b) {
4041
return b.toString(16).padStart(2, '0');

‎test/unit/validate.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ describe('validate', () => {
1313

1414
assert.strictEqual(validate('90123e1c-7512-523e-bb28-76fab9f2f73d'), true);
1515

16+
assert.strictEqual(validate(), false);
17+
18+
assert.strictEqual(validate(''), false);
19+
1620
assert.strictEqual(validate('invalid uuid string'), false);
1721

1822
assert.strictEqual(validate('00000000000000000000000000000000'), false);

‎test/unit/version.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ describe('version', () => {
1313

1414
assert.strictEqual(version('90123e1c-7512-523e-bb28-76fab9f2f73d'), 5);
1515

16+
assert.throws(() => version());
17+
18+
assert.throws(() => version(''));
19+
1620
assert.throws(() => version('invalid uuid string'));
1721

1822
assert.throws(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.