From 5ae7287fc935eb55ef39133e4be17ef623ca000e Mon Sep 17 00:00:00 2001 From: Roman Shtylman Date: Thu, 17 Nov 2016 21:52:45 -0800 Subject: [PATCH] remove .parse and .unparse API --- HISTORY.md | 2 ++ README.md | 29 ----------------------------- test/test.js | 9 --------- uuid.js | 37 ++++++++----------------------------- 4 files changed, 10 insertions(+), 67 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index ad681083..9f05c7a1 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,7 @@ # UNRELEASED + * remove .parse and .unparse + # 2.0.0 * Removed uuid.BufferClass diff --git a/README.md b/README.md index f1d772ec..def14baf 100644 --- a/README.md +++ b/README.md @@ -119,35 +119,6 @@ uuid.v4(null, buffer, 0); uuid.v4(null, buffer, 16); ``` -### uuid.parse(id[, buffer[, offset]]) -### uuid.unparse(buffer[, offset]) - -Parse and unparse UUIDs - - * `id` - (String) UUID(-like) string - * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written. Default: A new Array or Buffer is used - * `offset` - (Number) Starting index in `buffer` at which to begin writing. Default: 0 - -Example parsing and unparsing a UUID string - -```javascript -const bytes = uuid.parse('797ff043-11eb-11e1-80d6-510998755d10'); // -> -const string = uuid.unparse(bytes); // -> '797ff043-11eb-11e1-80d6-510998755d10' -``` - -### uuid.noConflict() - -(Browsers only) Set `uuid` property back to it's previous value. - -Returns the uuid object. - -Example: - -```javascript -const myUuid = uuid.noConflict(); -myUuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a' -``` - ## Testing ``` diff --git a/test/test.js b/test/test.js index 4c9872ff..ec33bec0 100644 --- a/test/test.js +++ b/test/test.js @@ -94,12 +94,3 @@ test('ids spanning 1ms boundary are 100ns apart', function() { var dt = parseInt(after, 16) - parseInt(before, 16); assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart'); }); - -test('parse/unparse', function() { - var id = '00112233445566778899aabbccddeeff'; - assert(uuid.unparse(uuid.parse(id.substr(0,10))) == - '00112233-4400-0000-0000-000000000000', 'Short parse'); - assert(uuid.unparse(uuid.parse('(this is the uuid -> ' + id + id)) == - '00112233-4455-6677-8899-aabbccddeeff', 'Dirty parse'); -}); - diff --git a/uuid.js b/uuid.js index 71156484..a1f28f75 100644 --- a/uuid.js +++ b/uuid.js @@ -6,33 +6,14 @@ var _rng = require('./lib/rng'); // Maps for number <-> hex string conversion var _byteToHex = []; var _hexToByte = {}; -for (var i = 0; i < 256; i++) { +for (var i = 0; i < 256; ++i) { _byteToHex[i] = (i + 0x100).toString(16).substr(1); _hexToByte[_byteToHex[i]] = i; } -// **`parse()` - Parse a UUID into it's component bytes** -function parse(s, buf, offset) { - var i = (buf && offset) || 0, ii = 0; - - buf = buf || []; - s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) { - if (ii < 16) { // Don't overflow! - buf[i + ii++] = _hexToByte[oct]; - } - }); - - // Zero out remaining bytes if string was short - while (ii < 16) { - buf[i + ii++] = 0; - } - - return buf; -} - -// **`unparse()` - Convert UUID byte array (ala parse()) into a string** -function unparse(buf, offset) { - var i = offset || 0, bth = _byteToHex; +function buff_to_string(buf, offset) { + var i = offset || 0; + var bth = _byteToHex; return bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] + '-' + @@ -132,11 +113,11 @@ function v1(options, buf, offset) { // `node` var node = options.node || _nodeId; - for (var n = 0; n < 6; n++) { + for (var n = 0; n < 6; ++n) { b[i + n] = node[n]; } - return buf ? buf : unparse(b); + return buf ? buf : buff_to_string(b); } // **`v4()` - Generate random UUID** @@ -160,19 +141,17 @@ function v4(options, buf, offset) { // Copy bytes to buffer, if provided if (buf) { - for (var ii = 0; ii < 16; ii++) { + for (var ii = 0; ii < 16; ++ii) { buf[i + ii] = rnds[ii]; } } - return buf || unparse(rnds); + return buf || buff_to_string(rnds); } // Export public API var uuid = v4; uuid.v1 = v1; uuid.v4 = v4; -uuid.parse = parse; -uuid.unparse = unparse; module.exports = uuid;