UUID (Version 8) Generator & Parser For Browser & CLI – uuid-v8

Category: Javascript | December 27, 2023
Author:hayageek
Views Total:75 views
Official Page:Go to website
Last Update:December 27, 2023
License:MIT

Preview:

UUID (Version 8) Generator & Parser For Browser & CLI – uuid-v8

Description:

uuid-v8 is an experimental JavaScript library that implements the proposed UUID Version 8 standard.

This new version of UUID aims to improve on previous versions by generating UUIDs based on timestamps instead of random values.

The JavaScript library provides simple APIs to generate, parse, and work with these time-based UUIDs both in browser and CLI environments.

How to use it:

1. Install and import the uuid-v8 library.

# NPM
$ npm install uuid-v8
// Import
import { uuidv8, UUID } from "uuid-v8";
// OR
const { uuidv8, UUID } = require("uuid-v8")

2. The uuid-v8 library generates UUIDs based on the current time or a passed timestamp. This allows generating ordered UUIDs useful for databases and other ordered storage.

// e.g. 07e70c1b-0507-8f3a-83b7-046f781bef31
uuidv8();
// Current datetime
// e.g. 07e70c1b-0507-823a-83b7-27e5cb791c76
UUID.generate();
// For a given time
// e.g. 07e70c1b-0507-8d3a-83b7-b2e0a9043ea6
UUID.generate({
  time: customDateTime
});

3. Get the timestamp back from a UUID.

// 2023-12-27T05:11:12.482Z
UUID.getTime('07e70c1b-050b-8f0c-81e2-3e2c2c769ea8');

4. Parsing UUIDs to bytes or hex values for storage or transmission.

// Uint8Array{0: 7, 1: 231, 2: 12, 3: 27, …}
UUID.parse('07e70c1b-050b-8f0c-81e2-3e2c2c769ea8');
// ["07", "e7", "0c", "1b", …]
UUID.parseToHex('07e70c1b-050b-8f0c-81e2-3e2c2c769ea8');

You Might Be Interested In:


Leave a Reply