Skip to content

Commit

Permalink
Revert "feat: short version of parsing UUID"
Browse files Browse the repository at this point in the history
This reverts commit d096cc2.
  • Loading branch information
ctavan committed May 30, 2020
1 parent 70f4179 commit f30b4a7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/uuid-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ 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 @@ -57,8 +62,13 @@ 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: 18 additions & 4 deletions src/v35.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import bytesToUuid from './bytesToUuid.js';
import validate from './validate.js';

// 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];
// 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;
}
}

function uuidToBytes(uuid) {
if (!validate(uuid)) {
throw TypeError('Invalid UUID');
return [];
}

return HEX_PAIRS.map((i) => parseInt(uuid.substr(i, 2), 16));
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;
}

function stringToBytes(str) {
Expand Down

0 comments on commit f30b4a7

Please sign in to comment.