@litert/uuid
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

LiteRT/UUID

npm version License node GitHub issues GitHub Releases

A uuid generator library for LiteRT framework.

Requirement

  • TypeScript v2.7.1 (or newer)
  • Node.js v7.0.0 (or newer)

Installation

Install by NPM:

npm i @litert/uuid --save

Algorithms

Following algorithms are supported:

Snowflake

A 64-bit integer UUID algorithm provided by Twitter.

Snowflake-SI

The Snowflake-SI algorithm generates safe integer (52-bits integer) for JavaScript, with capacity 128 uuids per milliseconds, and 64 machine parallel working.

Snowflake-SI-vA

A variant of Snowflake-SI algorithm.

Samples

Snowflake-SI Usage

import * as libuuid from "@litert/uuid";

const makeUUID = libuuid.SnowflakeSI.createGenerateor({

    /**
     * The machine ID.
     *
     * By default the uinWidth is 8, and the rest 5 bits is left to MID, so
     * that the mid must be between 0 and 31.
     */
    "mid": 1,

    /**
     * The base-clock of generator. Is unchangeable once setup.
     *
     * And it cannot be earlier than 2003-03-18T07:20:20Z
     */
    "baseClock": new Date(2004, 0, 1, 0, 0, 0, 0).getTime(),

});

console.log(makeUUID()); // Generate a UUIN
console.log(makeUUID()); // Generate a UUIN
console.log(makeUUID.MS_CAPACITY); // See the capacity of UUIDs in 1ms
console.log(makeUUID.MACHINE_ID); // See the machine ID.
console.log(new Date(makeUUID.BASE_CLOCK)); // See the base-clock

Snowflake-SI Adjustment

import * as libuuid from "@litert/uuid";

const makeUUID = libuuid.SnowflakeSI.createGenerateor({

    /**
     * The machine ID.
     *
     * Now the uinWidth has been set to 12, and only 1 bit is left to MID, so
     * that the mid can be either 1 or 0.
     */
    "mid": 1,

    /**
     * The base-clock of generator. Is unchangeable once setup.
     *
     * And it cannot be earlier than 2003-03-18T07:20:20Z
     */
    "baseClock": new Date(2004, 0, 1, 0, 0, 0, 0).getTime(),

    /**
     * The bit-width of UIN.
     */
    "uinWidth": 12
});

console.log(makeUUID()); // Generate a UUIN
console.log(makeUUID()); // Generate a UUIN
console.log(makeUUID.MS_CAPACITY); // See the capacity of UUIDs in 1ms
console.log(makeUUID.MACHINE_ID); // See the machine ID.
console.log(new Date(makeUUID.BASE_CLOCK)); // See the base-clock

Snowflake-SI-vA Usage

import * as libuuid from "@litert/uuid";

const makeUUID = libuuid.SnowflakeSIvA.createGenerateor({

    /**
     * The ID of machine, 0 ~ 1023
     */
    "mid": 333,

    /**
     * Calculate the cursor of the incremental sequence insides generator by
     * the last generated UUID.
     */
    "cursor": libuuid.SnowflakeSIvA.calculateCursor(1562244321456127)
});

console.log(nextUUID());
console.log(nextUUID());
console.log(nextUUID.MACHINE_ID);
console.log(Number.isSafeInteger(nextUUID()));

License

This library is published under Apache-2.0 license.

Package Sidebar

Install

npm i @litert/uuid

Weekly Downloads

5

Version

1.0.0

License

Apache-2.0

Unpacked Size

139 kB

Total Files

65

Last publish

Collaborators

  • fenying