3.2.2 • Published 12 days ago

@i-xi-dev/uuid v3.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
12 days ago

@i-xi-dev/uuid

A JavaScript UUID generator, implements the version 3, 4, and 5 UUID defined in RFC 4122. And this generator implements version 7 draft proposed in RFC 4122 bis.

Requirement

Uuid.generateRandom and Uuid.generateUnixTimeBased methods

This requires Crypto.

ChromeEdgeFirefoxSafariDenoNode.js
15.0.0+

Uuid.fromName methods

Generating version 5 requires SubtleCrypto.

ChromeEdgeFirefoxSafariDenoNode.js
15.0.0+

SubtleCrypto is available in secure contexts, in browsers.

Installation

npm

$ npm i @i-xi-dev/uuid@3.2.2
import { Uuid } from "@i-xi-dev/uuid";

CDN

Example for UNPKG

import { Uuid } from "https://www.unpkg.com/@i-xi-dev/uuid@3.2.2/esm/mod.js";

Usage

Uuid class

Creates a version 4 UUID

const uuid = Uuid.generateRandom();
// uuid.toString();
//   → for example "5eb893ba-ec79-4f55-958d-66731227b662"

// Gets the variant, and the version
// uuid.variant;
//   → 2
// uuid.version;
//   → 4

// Gets the URN
const urn = uuid.toURN();
// urn.toString();
//   → for example "urn:uuid:5eb893ba-ec79-4f55-958d-66731227b662"

Creates an instance from string

const uuidB = Uuid.fromString("5eb893ba-ec79-4f55-958d-66731227b662");
const uuidC = Uuid.fromString("5eb893baec794f55958d66731227b662");
const uuidD = Uuid.fromString("5EB893BA-EC79-4F55-958D-66731227B662");
const uuidE = Uuid.fromString("5EB893BAEC794F55958D66731227B662");
const uuidF = Uuid.fromString("urn:uuid:5eb893ba-ec79-4f55-958d-66731227b662");

// uuidB.equals(uuidC);
//   → true
// uuidB.equals(uuidD);
//   → true
// uuidB.equals(uuidE);
//   → true
// uuidB.equals(uuidF);
//   → true

// uuidB.equals("5eb893ba-ec79-4f55-958d-66731227b662");
//   → true
// uuidB.equals("5eb893baec794f55958d66731227b662");
//   → true
// uuidB.equals("5EB893BA-EC79-4F55-958D-66731227B662");
//   → true
// uuidB.equals("5EB893BAEC794F55958D66731227B662");
//   → true
// uuidB.equals("urn:uuid:5eb893ba-ec79-4f55-958d-66731227b662");
//   → true
const uuidX = Uuid.fromString(crypto.randomUUID());

Creates a version 5 UUID

const namespace = Uuid.Namespace.URL; // 6ba7b811-9dad-11d1-80b4-00c04fd430c8
const uuid = await Uuid.fromName(namespace, "https://example.com/sample/123");
// uuid.toString();
//   → "7fdb2afb-a771-50eb-a0ae-7f02b933a569"

Creates a version 3 UUID

const namespace = Uuid.Namespace.URL; // 6ba7b811-9dad-11d1-80b4-00c04fd430c8
const uuid = await Uuid.fromName(namespace, "https://example.com/sample/123", 3);
// uuid.toString();
//   → "b131a200-1fa6-313e-b5d2-6b7a9b00570c"

Creates a version 7 UUID proposal

const uuid = Uuid.generateUnixTimeBased();
// uuid.toString();
//   → for example "018d8e7b-f31f-7000-98b7-4593649d25e1"
// new Date(uuid.unixTimeMilliseconds).toISOString();
//   → for example "2024-02-09T15:28:24.351Z"

The precision of the time is the greater of the following:

  • 1 millisecond
  • The precision of the time of performance.now() in execution environment

Note that some browsers have performance.now() precision greater than 1 millisecond, depending on settings and other factors.

Examples

3.2.2

12 days ago

3.2.0

3 months ago

3.1.0

3 months ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.1

2 years ago

2.2.1

2 years ago

3.0.0

2 years ago

2.2.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.0

3 years ago