Skip to content

Commit

Permalink
feat: short version of parsing UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
awwit committed May 29, 2020
1 parent f52d4e8 commit d096cc2
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .babelrc.js
Expand Up @@ -6,7 +6,7 @@ module.exports = {
presets: [['@babel/preset-env', { targets: { node: '8' }, modules: 'commonjs' }]],
},
esmBrowser: {
presets: [['@babel/preset-env', { modules: false }]],
presets: [['@babel/preset-env', { targets: { ie: '11' }, modules: false }]],
},
esmNode: {
presets: [['@babel/preset-env', { targets: { node: '8' }, modules: false }]],
Expand Down
3 changes: 1 addition & 2 deletions .eslintrc.json
Expand Up @@ -13,7 +13,6 @@
},
"parser": "babel-eslint",
"rules": {
"no-var": ["error"],
"curly": ["error", "all"]
"no-var": ["error"]
}
}
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -292,7 +292,7 @@ ddeb27fb-d9a0-4624-be4d-4615062daed4
The default is to generate version 4 UUIDS, however the other versions are supported. Type
`uuid --help` for details:

```
```shell
$ uuid --help

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

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

```
```shell
npm run build
```

Expand Down
4 changes: 2 additions & 2 deletions README_js.md
Expand Up @@ -281,7 +281,7 @@ ddeb27fb-d9a0-4624-be4d-4615062daed4
The default is to generate version 4 UUIDS, however the other versions are supported. Type
`uuid --help` for details:

```
```shell
$ uuid --help

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

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

```
```shell
npm run build
```

Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Expand Up @@ -2,6 +2,6 @@ export { default as v1 } from './v1.js';
export { default as v3 } from './v3.js';
export { default as v4 } from './v4.js';
export { default as v5 } from './v5.js';
export { default as REGEX } from './regex.js';
export { default as version } from './version.js';
export { default as validate } from './validate.js';
export { default as getVersionUUID } from './version.js';
export { default as validateUUID } from './validate.js';
export { default as UUID_REGEXP } from './regex.js';
4 changes: 2 additions & 2 deletions src/regex.js
@@ -1,3 +1,3 @@
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;
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 REGEX;
export default UUID_REGEXP;
18 changes: 4 additions & 14 deletions src/uuid-bin.js
Expand Up @@ -39,13 +39,8 @@ switch (version) {
assert(name != null, 'v3 name not specified');
assert(namespace != null, 'v3 namespace not specified');

if (namespace === 'URL') {
namespace = v3.URL;
}

if (namespace === 'DNS') {
namespace = v3.DNS;
}
if (namespace === 'URL') namespace = v3.URL;
if (namespace === 'DNS') namespace = v3.DNS;

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

if (namespace === 'URL') {
namespace = v5.URL;
}

if (namespace === 'DNS') {
namespace = v5.DNS;
}
if (namespace === 'URL') namespace = v5.URL;
if (namespace === 'DNS') namespace = v5.DNS;

console.log(v5(name, namespace));
break;
Expand Down
22 changes: 4 additions & 18 deletions src/v35.js
@@ -1,29 +1,15 @@
import bytesToUuid from './bytesToUuid.js';
import validate from './validate.js';

// Int32 to 4 bytes https://stackoverflow.com/a/12965194/3684944
function numberToBytes(num, bytes, offset) {
for (let i = 0; i < 4; ++i) {
const byte = num & 0xff;
// Fill the 4 bytes right-to-left.
bytes[offset + 3 - i] = byte;
num = (num - byte) / 256;
}
}
// 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)) {
return [];
throw TypeError('Invalid UUID');
}

const bytes = new Array(16);

numberToBytes(parseInt(uuid.slice(0, 8), 16), bytes, 0);
numberToBytes(parseInt(uuid.slice(9, 13) + uuid.slice(14, 18), 16), bytes, 4);
numberToBytes(parseInt(uuid.slice(19, 23) + uuid.slice(24, 28), 16), bytes, 8);
numberToBytes(parseInt(uuid.slice(28), 16), bytes, 12);

return bytes;
return HEX_PAIRS.map((i) => parseInt(uuid.substr(i, 2), 16));
}

function stringToBytes(str) {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/v35.test.js
Expand Up @@ -6,7 +6,7 @@ import sha1Browser from '../../src/sha1-browser.js';
import v3 from '../../src/v3.js';
import v5 from '../../src/v5.js';

describe('v5', () => {
describe('v35', () => {
const HASH_SAMPLES = [
{
input: '',
Expand Down Expand Up @@ -35,6 +35,7 @@ describe('v5', () => {
if (hash instanceof Buffer) {
hash = Array.from(hash);
}

return hash
.map(function (b) {
return b.toString(16).padStart(2, '0');
Expand Down
4 changes: 4 additions & 0 deletions test/unit/validate.test.js
Expand Up @@ -13,6 +13,10 @@ describe('validate', () => {

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);
Expand Down
4 changes: 4 additions & 0 deletions test/unit/version.test.js
Expand Up @@ -13,6 +13,10 @@ describe('version', () => {

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

assert.throws(() => version());

assert.throws(() => version(''));

assert.throws(() => version('invalid uuid string'));

assert.throws(() => {
Expand Down

0 comments on commit d096cc2

Please sign in to comment.