Skip to content

An experimental implementation of the proposed UUID Version 8

License

Notifications You must be signed in to change notification settings

hayageek/uuid-v8

Repository files navigation

uuid-v8

An experimental implementation of the proposed UUID Version 8

Installation

npm install uuid-v8 --save

Default

import { uuidv8 } from "uuid-v8";

const result = uuidv8(); // e.g., "07e70c15-0f38-8bfc-ba65-f0ec85dc2812"
(or)
const { uuidv8 } = require("uuid-v8")

const result = uuidv8(); // e.g., "07e70c15-0f38-8bfc-ba65-f0ec85dc2812"

with custom time

import { UUID } from "uuid-v8";

//current Time
const uuid1 = UUID.generate(); // e.g., "07e70c16-0631-870e-0316-e51b1fab5ebb"

//Generate for a given time
var t = new Date()
const uuid2 = UUID.generate({time:t}); 

Get time associated with the UUID

import { UUID } from "uuid-v8";

//Gets the time associated with the UUID
const date = UUID.getTime('07e70c16-0631-870e-0316-e51b1fab5ebb'); 

Parse the UUID to Bytes

import { UUID } from "uuid-v8";

//Gets the time associated with the UUID
const bytes = UUID.parse('07e70c16-0631-870e-0316-e51b1fab5ebb'); 

const bytesInHex = UUID.parseToHex('07e70c16-0631-870e-0316-e51b1fab5ebb'); 

Command-line interface:

$ npx uuid-v8
07e70c16-0637-8003-0139-e65bfe87ae09
$
$ npx uuid-v8 -n 4
07e70c16-0637-8e13-0085-98ded92d312c
07e70c16-0637-8713-0087-558d00e6ffa1
07e70c16-0637-8e13-0087-8deb1113ab9e
07e70c16-0637-8313-0088-bdc6ff36d2fc
07e70c16-0637-8713-0088-02fbd0573c85

See draft-ietf-uuidrev-rfc4122bis-11.

Field and bit layout

UUID version 8 provides an RFC-compatible format for experimental or vendor-specific use cases. The only requirement is that the variant and version bits MUST be set as defined in Section 4.1 and Section 4.2. UUIDv8's uniqueness will be implementation-specific and MUST NOT be assumed.The only explicitly defined bits are those of the version and variant fields, leaving 122 bits for implementation specific UUIDs. To be clear: UUIDv8 is not a replacement for UUIDv4 Section 5.4 where all 122 extra bits are filled with random data.Some example situations in which UUIDv8 usage could occur: An implementation would like to embed extra information within the UUID other than what is defined in this document. An implementation has other application/language restrictions which inhibit the use of one of the current UUIDs.

 0                   10                  20                  30
 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        year-month-day                         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          hour:minute          |  ver  | rand  |    seconds    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var| milliseconds  |                   rand                    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             rand                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

License

The MIT License (MIT)

External Links

About

An experimental implementation of the proposed UUID Version 8

Resources

License

Stars

Watchers

Forks

Packages

No packages published