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

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../v7.js

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
dist/
33
node_modules/
44
README.md
5+
*.sh

AUTHORS

+1
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

+26-1
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

+26-1
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

+4-1
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

+14
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

+15-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/browser-esmodules/example.js

+4
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

+2-1
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
```

examples/browser-rollup/example-all.js

+4
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 'uuid';
@@ -20,6 +21,8 @@ testpage(function (addTest, done) {
2021

2122
addTest('uuidv4()', uuidv4());
2223

24+
addTest('uuidv7()', uuidv7());
25+
2326
// ... using predefined DNS namespace (for domain names)
2427
addTest('uuidv3() DNS', uuidv3('hello.example.com', uuidv3.DNS));
2528

@@ -57,6 +60,7 @@ testpage(function (addTest, done) {
5760

5861
addTest('uuid.v1()', uuid.v1());
5962
addTest('uuid.v4()', uuid.v4());
63+
addTest('uuid.v7()', uuid.v7());
6064
addTest('uuid.v3() DNS', uuid.v3('hello.example.com', uuid.v3.DNS));
6165
addTest('uuid.v3() URL', uuid.v3('http://example.com/hello', uuid.v3.URL));
6266
addTest('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>UUID esmodule webpack example</title>
5+
</head>
6+
7+
<body>
8+
<script type="text/javascript" src="./dist/v7.js"></script>
9+
</body>
10+
</html>

examples/browser-rollup/example-v7.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { v7 as uuidv7 } from 'uuid';
2+
3+
import testpage from '../utils/testpage';
4+
5+
testpage(function (addTest, done) {
6+
addTest('uuidv7()', uuidv7());
7+
done();
8+
});

examples/browser-rollup/example.html

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<p>Please open the Developer Console to view output</p>
44
<script type="text/javascript" src="./dist/v1.js"></script>
55
<script type="text/javascript" src="./dist/v4.js"></script>
6+
<script type="text/javascript" src="./dist/v7.js"></script>
67
<script type="text/javascript" src="./dist/all.js"></script>

examples/browser-rollup/rollup.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ module.exports = [
2727
},
2828
plugins,
2929
},
30+
{
31+
input: './example-v7.js',
32+
output: {
33+
file: 'dist/v7.js',
34+
format: 'iife',
35+
},
36+
plugins,
37+
},
3038

3139
{
3240
input: './size-v1.js',
@@ -60,4 +68,12 @@ module.exports = [
6068
},
6169
plugins,
6270
},
71+
{
72+
input: './size-v7.js',
73+
output: {
74+
file: 'dist/v7-size.js',
75+
format: 'cjs',
76+
},
77+
plugins,
78+
},
6379
];

examples/browser-rollup/size-v7.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { v7 as uuidv7 } from 'uuid';
2+
3+
uuidv7();

examples/browser-webpack/example-all-require.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
v3: uuidv3,
88
v4: uuidv4,
99
v5: uuidv5,
10+
v7: uuidv7,
1011
validate: uuidValidate,
1112
version: uuidVersion,
1213
} = uuid;
@@ -20,6 +21,8 @@ testpage(function (addTest, done) {
2021

2122
addTest('uuidv4()', uuidv4());
2223

24+
addTest('uuidv7()', uuidv7());
25+
2326
// ... using predefined DNS namespace (for domain names)
2427
addTest('uuidv3() DNS', uuidv3('hello.example.com', uuidv3.DNS));
2528

@@ -57,6 +60,7 @@ testpage(function (addTest, done) {
5760

5861
addTest('uuid.v1()', uuid.v1());
5962
addTest('uuid.v4()', uuid.v4());
63+
addTest('uuid.v7()', uuid.v7());
6064
addTest('uuid.v3() DNS', uuid.v3('hello.example.com', uuid.v3.DNS));
6165
addTest('uuid.v3() URL', uuid.v3('http://example.com/hello', uuid.v3.URL));
6266
addTest('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));

0 commit comments

Comments
 (0)