LiosK / uuidv7

A JavaScript implementation of UUID version 7
Apache License 2.0
177 stars 3 forks source link

[Feature request] create uuidv7 for a specific time #3

Closed jperelli closed 1 year ago

jperelli commented 1 year ago

I want to migrate from uuidv4 to uuidv7, but I want to keep the timestamp info that uuidv7 has. I have objects with createdAt timestamp, and I want to use those to migrate. I think this would be a great addition to this package, and seems to be simple (not really sure about this :))

The signature could be something like uuidFromDate(new Date(2020-10-02)) and it could internally call the function UUID.fromFieldsV7. I tried to implement it with that function but failed miserably to understand the parameters.

This might be also useful for testing purposes

Thanks for the library, I'm using it in a project, very useful!!

LiosK commented 1 year ago

Implemented this feature experimentally. Please try the following code with v0.5.3.

import { V7Generator } from "uuidv7";

const g = V7Generator.create();
const timestamp = 0x0123_4567_8901;
console.log(g.generateOrResetCore(timestamp, 1).toString());
console.log(g.generateOrResetCore(timestamp, 1).toString());
console.log(g.generateOrResetCore(timestamp, 1).toString());
console.log(g.generateOrResetCore(timestamp, 1).toString());