diff --git a/.babelrc.js b/.babelrc.js index f42a05a4..03943e8d 100644 --- a/.babelrc.js +++ b/.babelrc.js @@ -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 }]], diff --git a/.eslintrc.json b/.eslintrc.json index b329c54a..cd838aeb 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -13,7 +13,6 @@ }, "parser": "babel-eslint", "rules": { - "no-var": ["error"], - "curly": ["error", "all"] + "no-var": ["error"] } } diff --git a/README.md b/README.md index f683ca56..7390813b 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 ``` diff --git a/README_js.md b/README_js.md index bdbb1b75..e16e45f4 100644 --- a/README_js.md +++ b/README_js.md @@ -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: @@ -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 ``` diff --git a/src/index.js b/src/index.js index 7702f92a..051c8123 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; diff --git a/src/regex.js b/src/regex.js index 4ed878b6..5c2709c9 100644 --- a/src/regex.js +++ b/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; diff --git a/src/uuid-bin.js b/src/uuid-bin.js index f7305adc..0a82fd27 100644 --- a/src/uuid-bin.js +++ b/src/uuid-bin.js @@ -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; @@ -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; diff --git a/src/v35.js b/src/v35.js index dc7775fe..a0f68ab6 100644 --- a/src/v35.js +++ b/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) { diff --git a/test/unit/v35.test.js b/test/unit/v35.test.js index 938e94a9..b6d9312f 100644 --- a/test/unit/v35.test.js +++ b/test/unit/v35.test.js @@ -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: '', @@ -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'); diff --git a/test/unit/validate.test.js b/test/unit/validate.test.js index ef2f85ac..12980ae5 100644 --- a/test/unit/validate.test.js +++ b/test/unit/validate.test.js @@ -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); diff --git a/test/unit/version.test.js b/test/unit/version.test.js index ae9385bf..32c18261 100644 --- a/test/unit/version.test.js +++ b/test/unit/version.test.js @@ -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(() => {