Skip to content

Commit 70eccee

Browse files
committed
🐛 uuidv7 rollover
1 parent f4e4426 commit 70eccee

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/uuid.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export function v4(): string {
1919
* rand_b:
2020
* The final 62 bits of pseudo-random data to provide uniqueness as per Section 6.2 and Section 6.6.
2121
*/
22-
let seq = 0;
2322
export function v7(): string {
2423
const timeComponent = Date.now().toString(16).padStart(12, "0");
2524

@@ -33,14 +32,20 @@ export function v7(): string {
3332
"-" +
3433
timeComponent.slice(8, 12) +
3534
"-7" +
36-
(seq++).toString(16).padStart(3, "0") +
35+
nextSeq().toString(16).padStart(3, "0") +
3736
"-" +
3837
rndArrHex.slice(0, 4) +
3938
"-" +
4039
rndArrHex.slice(4, 16)
4140
);
4241
}
4342

43+
let seq = 0;
44+
function nextSeq() {
45+
seq = seq === 0xfff ? 0 : seq + 1;
46+
return seq;
47+
}
48+
4449
export function fromBuffer(buffer: Buffer): string {
4550
const hex = buffer.toString("hex");
4651
return (

0 commit comments

Comments
 (0)