Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support v6 uuids #754

Merged
merged 23 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node_modules/
dist/
# Browserstack
*.tgz
browserstack.err
dist/
local.log
*.tgz
node_modules/
vscode/
1 change: 1 addition & 0 deletions .local/uuid/v1tov6.js
1 change: 1 addition & 0 deletions .local/uuid/v6.js
1 change: 1 addition & 0 deletions .local/uuid/v6tov1.js
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
dist/
node_modules/
README.md
*.sh
*.sh
.gitignore
.prettierignore
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ For timestamp UUIDs, namespace UUIDs, and other options read on ...
| [`uuid.parse()`](#uuidparsestr) | Convert UUID string to array of bytes | New in `[email protected]` |
| [`uuid.stringify()`](#uuidstringifyarr-offset) | Convert array of bytes to UUID string | New in `[email protected]` |
| [`uuid.v1()`](#uuidv1options-buffer-offset) | Create a version 1 (timestamp) UUID | |
| [`uuid.v1ToV6()`](#uuidv7options-buffer-offset) | Create a version 6 UUID from a version 1 UUID | New in `uuid@10` |
| [`uuid.v3()`](#uuidv3name-namespace-buffer-offset) | Create a version 3 (namespace w/ MD5) UUID | |
| [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | |
| [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | |
| [`uuid.v7()`](#uuidv7options-buffer-offset) | Create a version 7 (Unix Epoch time-based) UUID | `experimental support` |
| [`uuid.v6()`](#uuidv5name-namespace-buffer-offset) | Create a version 6 (timestamp, reordered) UUID | New in `uuid@10` |
| [`uuid.v6ToV1()`](#uuidv7options-buffer-offset) | Create a version 1 UUID from a version 6 UUID | New in `uuid@10` |
| [`uuid.v7()`](#uuidv7options-buffer-offset) | Create a version 7 (Unix Epoch time-based) UUID | New in `uuid@10` |
| [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `[email protected]` |
| [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `[email protected]` |

Expand Down Expand Up @@ -200,6 +203,16 @@ const v1options = {
uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
```

### uuid.v1ToV6(uuid)

Convert a UUID from version 1 to version 6

```javascript
import { v1ToV6 } from 'uuid';

v1ToV6('92f62d9e-22c4-11ef-97e9-325096b39f47'); // ⇨ '1ef22c49-2f62-6d9e-97e9-325096b39f47'
```

### uuid.v3(name, namespace[, buffer[, offset]])

Create an RFC version 3 (namespace w/ MD5) UUID
Expand Down Expand Up @@ -280,6 +293,42 @@ import { v5 as uuidv5 } from 'uuid';
uuidv5('https://www.w3.org/', uuidv5.URL); // ⇨ 'c106a26a-21bb-5538-8bf2-57095d1976c1'
```

### uuid.v6([options, [, buffer[, offset]]])

Create an RFC version 6 (timestamp, reordered) UUID

This method takes the same args as uuid.v1().

```javascript
import { v6 as uuidv6 } from 'uuid';

uuidv6(); // ⇨ '1e940672-c5ea-64c0-8bad-9b1deb4d3b7d'
```

Example using `options`:

```javascript
import { v6 as uuidv6 } from 'uuid';

const v1options = {
broofa marked this conversation as resolved.
Show resolved Hide resolved
node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
clockseq: 0x1234,
msecs: new Date('2011-11-01').getTime(),
nsecs: 5678,
};
uuidv6(v1options); // ⇨ '1e1041c7-10b9-662e-9234-0123456789ab'
```

### uuid.v6ToV1(uuid)

Convert a UUID from version 6 to version 1

```javascript
import { v6ToV1 } from 'uuid';

v6ToV1('92f62d9e-22c4-11ef-97e9-325096b39f47'); // ⇨ 'e22c41ef-62d9-192f-97e9-325096b39f47'
```

### uuid.v7([options[, buffer[, offset]]])

Create an RFC version 7 (random) UUID
Expand All @@ -300,7 +349,7 @@ Example:
```javascript
import { v7 as uuidv7 } from 'uuid';

uuidv7(); // ⇨ '01695553-c90c-7aad-9bdd-330d7b3dcb6d'
uuidv7(); // ⇨ '01695553-c90c-722d-9b5d-b38dfbbd4bed'
```

### uuid.validate(str)
Expand Down
51 changes: 50 additions & 1 deletion README_js.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ For timestamp UUIDs, namespace UUIDs, and other options read on ...
| [`uuid.parse()`](#uuidparsestr) | Convert UUID string to array of bytes | New in `[email protected]` |
| [`uuid.stringify()`](#uuidstringifyarr-offset) | Convert array of bytes to UUID string | New in `[email protected]` |
| [`uuid.v1()`](#uuidv1options-buffer-offset) | Create a version 1 (timestamp) UUID | |
| [`uuid.v1ToV6()`](#uuidv7options-buffer-offset) | Create a version 6 UUID from a version 1 UUID | New in `uuid@10` |
| [`uuid.v3()`](#uuidv3name-namespace-buffer-offset) | Create a version 3 (namespace w/ MD5) UUID | |
| [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | |
| [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | |
| [`uuid.v7()`](#uuidv7options-buffer-offset) | Create a version 7 (Unix Epoch time-based) UUID | `experimental support` |
| [`uuid.v6()`](#uuidv5name-namespace-buffer-offset) | Create a version 6 (timestamp, reordered) UUID | New in `uuid@10` |
| [`uuid.v6ToV1()`](#uuidv7options-buffer-offset) | Create a version 1 UUID from a version 6 UUID | New in `uuid@10` |
| [`uuid.v7()`](#uuidv7options-buffer-offset) | Create a version 7 (Unix Epoch time-based) UUID | New in `uuid@10` |
| [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `[email protected]` |
| [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `[email protected]` |

Expand Down Expand Up @@ -209,6 +212,16 @@ const v1options = {
uuidv1(v1options); // RESULT
```

### uuid.v1ToV6(uuid)

Convert a UUID from version 1 to version 6

```javascript --run
import { v1ToV6 } from 'uuid';

v1ToV6('92f62d9e-22c4-11ef-97e9-325096b39f47'); // RESULT
```

### uuid.v3(name, namespace[, buffer[, offset]])

Create an RFC version 3 (namespace w/ MD5) UUID
Expand Down Expand Up @@ -289,6 +302,42 @@ import { v5 as uuidv5 } from 'uuid';
uuidv5('https://www.w3.org/', uuidv5.URL); // RESULT
```

### uuid.v6([options, [, buffer[, offset]]])

Create an RFC version 6 (timestamp, reordered) UUID

This method takes the same args as uuid.v1().

```javascript --run
import { v6 as uuidv6 } from 'uuid';

uuidv6(); // RESULT
```

Example using `options`:

```javascript --run
import { v6 as uuidv6 } from 'uuid';

const v1options = {
node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
clockseq: 0x1234,
msecs: new Date('2011-11-01').getTime(),
nsecs: 5678,
};
uuidv6(v1options); // RESULT
```

### uuid.v6ToV1(uuid)

Convert a UUID from version 6 to version 1

```javascript --run
import { v6ToV1 } from 'uuid';

v6ToV1('1ef22c49-2f62-6d9e-97e9-325096b39f47'); // RESULT
```

### uuid.v7([options[, buffer[, offset]]])

Create an RFC version 7 (random) UUID
Expand Down
43 changes: 10 additions & 33 deletions bundlewatch.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,17 @@
"trackBranches": ["main"]
},
"files": [
{
"path": "./examples/browser-rollup/dist/v1-size.js",
"maxSize": "1.0 kB"
},
{
"path": "./examples/browser-rollup/dist/v3-size.js",
"maxSize": "2.1 kB"
},
{
"path": "./examples/browser-rollup/dist/v4-size.js",
"maxSize": "0.7 kB"
},
{
"path": "./examples/browser-rollup/dist/v5-size.js",
"maxSize": "1.5 kB"
},
{ "path": "./examples/browser-rollup/dist/v1-size.js", "maxSize": "1.0 kB" },
{ "path": "./examples/browser-rollup/dist/v3-size.js", "maxSize": "2.1 kB" },
{ "path": "./examples/browser-rollup/dist/v4-size.js", "maxSize": "0.7 kB" },
{ "path": "./examples/browser-rollup/dist/v5-size.js", "maxSize": "1.5 kB" },
{ "path": "./examples/browser-rollup/dist/v6-size.js", "maxSize": "1.6 kB" },
{ "path": "./examples/browser-rollup/dist/v7-size.js", "maxSize": "0.8 kB" },

{
"path": "./examples/browser-webpack/dist/v1-size.js",
"maxSize": "1.0 kB"
},
{
"path": "./examples/browser-webpack/dist/v3-size.js",
"maxSize": "2.1 kB"
},
{
"path": "./examples/browser-webpack/dist/v4-size.js",
"maxSize": "0.7 kB"
},
{
"path": "./examples/browser-webpack/dist/v5-size.js",
"maxSize": "1.5 kB"
},
{ "path": "./examples/browser-webpack/dist/v1-size.js", "maxSize": "1.0 kB" },
{ "path": "./examples/browser-webpack/dist/v3-size.js", "maxSize": "2.1 kB" },
{ "path": "./examples/browser-webpack/dist/v4-size.js", "maxSize": "0.7 kB" },
{ "path": "./examples/browser-webpack/dist/v5-size.js", "maxSize": "1.5 kB" },
{ "path": "./examples/browser-webpack/dist/v6-size.js", "maxSize": "1.6 kB" },
{ "path": "./examples/browser-webpack/dist/v7-size.js", "maxSize": "0.8 kB" }
]
}
31 changes: 31 additions & 0 deletions examples/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export default function benchmark(uuid, Benchmark) {
.add('uuid.v5()', function () {
uuid.v5('hello.example.com', uuid.v5.DNS);
})
.add('uuid.v6()', function () {
uuid.v6();
})
.add('uuid.v7()', function () {
uuid.v7();
})
Expand All @@ -82,10 +85,38 @@ export default function benchmark(uuid, Benchmark) {
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
console.log('---\n');
})
.run();
}

function testV6Conversion() {
const suite = new Benchmark.Suite({
onError(event) {
console.error(event.target.error);
},
});

const V1_ID = 'f1207660-21d2-11ef-8c4f-419efbd44d48';
const V6_ID = '1ef21d2f-1207-6660-8c4f-419efbd44d48';

suite
.add('uuid.v1ToV6()', function () {
uuid.v1ToV6(V1_ID);
})
.add('uuid.v1ToV6() w/ randomization', function () {
uuid.v1ToV6(V1_ID, true);
})
.add('uuid.v6ToV1()', function () {
uuid.v6ToV1(V6_ID);
})
.on('cycle', function (event) {
console.log(event.target.toString());
})
.run();
}

testParseAndStringify();
testGeneration();
testV6Conversion();
}
14 changes: 14 additions & 0 deletions examples/benchmark/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 23 additions & 4 deletions examples/browser-esmodules/example.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import pkg from 'uuid/package.json';
import * as uuid from './node_modules/uuid/dist/esm-browser/index.js';
import {
NIL as NIL_UUID,
MAX as MAX_UUID,
NIL as NIL_UUID,
parse as uuidParse,
stringify as uuidStringify,
validate as uuidValidate,
version as uuidVersion,
v1 as uuidv1,
v1ToV6 as uuidv1ToV6,
v3 as uuidv3,
v4 as uuidv4,
v5 as uuidv5,
v6 as uuidv6,
v6ToV1 as uuidv6ToV1,
v7 as uuidv7,
validate as uuidValidate,
version as uuidVersion,
} from './node_modules/uuid/dist/esm-browser/index.js';
import * as uuid from './node_modules/uuid/dist/esm-browser/index.js';

console.log('uuidv1()', uuidv1());

Expand Down Expand Up @@ -45,6 +49,14 @@ console.log('uuidv5() URL', uuidv5('http://example.com/hello', uuidv5.URL));
// const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
console.log('uuidv5() MY_NAMESPACE', uuidv5('Hello, World!', MY_NAMESPACE));

console.log('uuidv6()', uuidv6());

// v6 <-> v1 conversion
const V1_ID = 'f1207660-21d2-11ef-8c4f-419efbd44d48';
const V6_ID = '1ef21d2f-1207-6660-8c4f-419efbd44d48';
console.log('uuidv1ToV6()', uuidv1ToV6(V1_ID));
console.log('uuidv6ToV1()', uuidv6ToV1(V6_ID));

// Utility functions
console.log('NIL_UUID', NIL_UUID);
console.log('MAX_UUID', MAX_UUID);
Expand All @@ -64,10 +76,17 @@ console.log('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));
console.log('uuid.v5() DNS', uuid.v5('hello.example.com', uuid.v5.DNS));
console.log('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
console.log('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));
console.log('uuid.v6()', uuid.v6());

console.log('uuid.v1ToV6()', uuid.v1ToV6(V1_ID));
console.log('uuid.v6ToV1()', uuid.v6ToV1(V6_ID));

console.log('uuid.NIL', uuid.NIL);
console.log('uuid.MAX', uuid.MAX);
console.log('uuid.parse()', uuid.parse(MY_NAMESPACE));
console.log('uuid.stringify()', uuid.stringify(uuid.parse(MY_NAMESPACE)));
console.log('uuid.validate()', uuid.validate(MY_NAMESPACE));
console.log('uuid.version()', uuid.version(MY_NAMESPACE));

// Some tools like react-native need to introspect the package.json file
console.log('pkg.name', pkg.name);
Loading
Loading