Skip to content

Commit db76a12

Browse files
authored
uuid v7 (#681)
1 parent bc46e19 commit db76a12

39 files changed

+549
-20
lines changed

.local/uuid/v7.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../v7.js

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
dist/
33
node_modules/
44
README.md
5+
*.sh

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Christoph Tavan <[email protected]>
33
44
Vincent Voyer <[email protected]>
55
Roman Shtylman <[email protected]>
6+
Patrick McCarren <[email protected]>

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
For the creation of [RFC4122](https://www.ietf.org/rfc/rfc4122.txt) UUIDs
99

10-
- **Complete** - Support for RFC4122 version 1, 3, 4, and 5 UUIDs
10+
- **Complete** - Support for RFC4122 version 1, 3, 4, 5, and 7 UUIDs
1111
- **Cross-platform** - Support for ...
1212
- CommonJS, [ECMAScript Modules](#ecmascript-modules) and [CDN builds](#cdn-builds)
1313
- NodeJS 12+ ([LTS releases](https://github.com/nodejs/Release))
@@ -59,6 +59,7 @@ For timestamp UUIDs, namespace UUIDs, and other options read on ...
5959
| [`uuid.v3()`](#uuidv3name-namespace-buffer-offset) | Create a version 3 (namespace w/ MD5) UUID | |
6060
| [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | |
6161
| [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | |
62+
| [`uuid.v7()`](#uuidv7options-buffer-offset) | Create a version 7 (Unix Epoch time-based) UUID | `experimental support` |
6263
| [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `[email protected]` |
6364
| [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `[email protected]` |
6465

@@ -251,6 +252,29 @@ import { v5 as uuidv5 } from 'uuid';
251252
uuidv5('https://www.w3.org/', uuidv5.URL); // ⇨ 'c106a26a-21bb-5538-8bf2-57095d1976c1'
252253
```
253254

255+
### uuid.v7([options[, buffer[, offset]]])
256+
257+
Create an RFC version 7 (random) UUID
258+
259+
| | |
260+
| --- | --- |
261+
| [`options`] | `Object` with one or more of the following properties: |
262+
| [`options.msecs`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) |
263+
| [`options.random`] | `Array` of 16 random bytes (0-255) |
264+
| [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
265+
| [`options.seq`] | 31 bit monotonic sequence counter as `Number` between 0 - 0x7fffffff |
266+
| [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
267+
| [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
268+
| _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
269+
270+
Example:
271+
272+
```javascript
273+
import { v7 as uuidv7 } from 'uuid';
274+
275+
uuidv7(); // ⇨ '01695553-c90c-7aad-9bdd-330d7b3dcb6d'
276+
```
277+
254278
### uuid.validate(str)
255279

256280
Test a string to see if it is a valid UUID
@@ -325,6 +349,7 @@ Usage:
325349
uuid v3 <name> <namespace uuid>
326350
uuid v4
327351
uuid v5 <name> <namespace uuid>
352+
uuid v7
328353
uuid --help
329354

330355
Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs

README_js.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require('crypto').randomUUID = undefined;
2121

2222
For the creation of [RFC4122](https://www.ietf.org/rfc/rfc4122.txt) UUIDs
2323

24-
- **Complete** - Support for RFC4122 version 1, 3, 4, and 5 UUIDs
24+
- **Complete** - Support for RFC4122 version 1, 3, 4, 5, and 7 UUIDs
2525
- **Cross-platform** - Support for ...
2626
- CommonJS, [ECMAScript Modules](#ecmascript-modules) and [CDN builds](#cdn-builds)
2727
- NodeJS 12+ ([LTS releases](https://github.com/nodejs/Release))
@@ -73,6 +73,7 @@ For timestamp UUIDs, namespace UUIDs, and other options read on ...
7373
| [`uuid.v3()`](#uuidv3name-namespace-buffer-offset) | Create a version 3 (namespace w/ MD5) UUID | |
7474
| [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | |
7575
| [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | |
76+
| [`uuid.v7()`](#uuidv7options-buffer-offset) | Create a version 7 (Unix Epoch time-based) UUID | `experimental support` |
7677
| [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `[email protected]` |
7778
| [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `[email protected]` |
7879

@@ -259,6 +260,29 @@ import { v5 as uuidv5 } from 'uuid';
259260
uuidv5('https://www.w3.org/', uuidv5.URL); // RESULT
260261
```
261262

263+
### uuid.v7([options[, buffer[, offset]]])
264+
265+
Create an RFC version 7 (random) UUID
266+
267+
| | |
268+
| --- | --- |
269+
| [`options`] | `Object` with one or more of the following properties: |
270+
| [`options.msecs`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) |
271+
| [`options.random`] | `Array` of 16 random bytes (0-255) |
272+
| [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
273+
| [`options.seq`] | 31 bit monotonic sequence counter as `Number` between 0 - 0x7fffffff |
274+
| [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
275+
| [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
276+
| _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
277+
278+
Example:
279+
280+
```javascript --run
281+
import { v7 as uuidv7 } from 'uuid';
282+
283+
uuidv7(); // RESULT
284+
```
285+
262286
### uuid.validate(str)
263287

264288
Test a string to see if it is a valid UUID
@@ -333,6 +357,7 @@ Usage:
333357
uuid v3 <name> <namespace uuid>
334358
uuid v4
335359
uuid v5 <name> <namespace uuid>
360+
uuid v7
336361
uuid --help
337362

338363
Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs

bundlewatch.config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"path": "./examples/browser-rollup/dist/v5-size.js",
2121
"maxSize": "1.5 kB"
2222
},
23+
{ "path": "./examples/browser-rollup/dist/v7-size.js", "maxSize": "0.8 kB" },
24+
2325
{
2426
"path": "./examples/browser-webpack/dist/v1-size.js",
2527
"maxSize": "1.0 kB"
@@ -35,6 +37,7 @@
3537
{
3638
"path": "./examples/browser-webpack/dist/v5-size.js",
3739
"maxSize": "1.5 kB"
38-
}
40+
},
41+
{ "path": "./examples/browser-webpack/dist/v7-size.js", "maxSize": "0.8 kB" }
3942
]
4043
}

examples/benchmark/benchmark.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,26 @@ export default function benchmark(uuid, Benchmark) {
5757
.add('uuid.v4() fill existing array', function () {
5858
uuid.v4(null, array, 0);
5959
})
60+
.add('uuid.v4() without native generation', function () {
61+
uuid.v4({}); // passing an object instead of null bypasses native.randomUUID
62+
})
6063
.add('uuid.v3()', function () {
6164
uuid.v3('hello.example.com', uuid.v3.DNS);
6265
})
6366
.add('uuid.v5()', function () {
6467
uuid.v5('hello.example.com', uuid.v5.DNS);
6568
})
69+
.add('uuid.v7()', function () {
70+
uuid.v7();
71+
})
72+
.add('uuid.v7() fill existing array', function () {
73+
uuid.v7(null, array, 0);
74+
})
75+
.add('uuid.v7() with defined time', function () {
76+
uuid.v7({
77+
msecs: 1645557742000,
78+
});
79+
})
6680
.on('cycle', function (event) {
6781
console.log(event.target.toString());
6882
})

examples/benchmark/package-lock.json

Lines changed: 15 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/browser-esmodules/example.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
v3 as uuidv3,
77
v4 as uuidv4,
88
v5 as uuidv5,
9+
v7 as uuidv7,
910
validate as uuidValidate,
1011
version as uuidVersion,
1112
} from './node_modules/uuid/dist/esm-browser/index.js';
@@ -15,6 +16,8 @@ console.log('uuidv1()', uuidv1());
1516

1617
console.log('uuidv4()', uuidv4());
1718

19+
console.log('uuidv7()', uuidv7());
20+
1821
// ... using predefined DNS namespace (for domain names)
1922
console.log('uuidv3() DNS', uuidv3('hello.example.com', uuidv3.DNS));
2023

@@ -52,6 +55,7 @@ console.log('Same with default export');
5255

5356
console.log('uuid.v1()', uuid.v1());
5457
console.log('uuid.v4()', uuid.v4());
58+
console.log('uuid.v7()', uuid.v7());
5559
console.log('uuid.v3() DNS', uuid.v3('hello.example.com', uuid.v3.DNS));
5660
console.log('uuid.v3() URL', uuid.v3('http://example.com/hello', uuid.v3.URL));
5761
console.log('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));

examples/browser-rollup/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ npm start
77

88
Then navigate to `example-*.html`.
99

10-
The `example-v{1,4}.js` demonstrate that treeshaking works as expected:
10+
The `example-v{1,4,7}.js` demonstrate that treeshaking works as expected:
1111

1212
```
1313
$ du -sh dist/*
1414
20K dist/all.js
1515
8.0K dist/v1.js
1616
4.0K dist/v4.js
17+
4.0K dist/v7.js
1718
```

0 commit comments

Comments
 (0)