Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
remove .parse and .unparse API
Browse files Browse the repository at this point in the history
  • Loading branch information
defunctzombie committed Nov 18, 2016
1 parent 3b3c93d commit 5ae7287
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 67 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# UNRELEASED

* remove .parse and .unparse

# 2.0.0

* Removed uuid.BufferClass
Expand Down
29 changes: 0 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'); // -> <Buffer 79 7f f0 43 11 eb 11 e1 80 d6 51 09 98 75 5d 10>
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

```
Expand Down
9 changes: 0 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

37 changes: 8 additions & 29 deletions uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++]] + '-' +
Expand Down Expand Up @@ -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**
Expand All @@ -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;

0 comments on commit 5ae7287

Please sign in to comment.