Giter Club home page Giter Club logo

ulidx's Introduction

ulidx

ULID generator for NodeJS and the browser

ulidx Tests status

ULID generator library, based off of the original ulid for NodeJS and the browser. ULIDs are Universally Unique Lexicographically Sortable Identifiers. This library adheres to this specification.

The original ulid is no longer maintained, and has several outstanding compatibility-related issues that were never addressed. This library aims to address those and remain compatible in a larger range of environments.

Installation

Install using npm by running: npm install ulidx --save.

ulidx provides types and is written entirely in Typescript.

Usage

Import ulid to generate new ULIDs:

import { ulid } from "ulidx";

ulid(); // 01F7DKCVCVDZN1Z5Q4FWANHHCC

Time seed

You can also provide a time seed which will consistently give you the same string for the time component.

This is useful for migrating to ulid.

ulid(1469918176385); // 01ARYZ6S41TSV4RRFFQ69G5FAV

Monotonic ULID factory

To generate monotonically increasing ULIDs, create a monotonic counter using the factory:

import { monotonicFactory } from "ulidx";

const ulid = monotonicFactory();

// Strict ordering for the same timestamp, by incrementing the least-significant random bit by 1
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVR8
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVR9
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVRA
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVRB
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVRC

// Even if a lower timestamp is passed (or generated), it will preserve sort order
ulid(100000); // 000XAL6S41ACTAV9WEVGEMMVRD

Decode ULID Time

Import decodeTime to extract the timestamp embedded in a ULID:

import { decodeTime } from "ulidx";

// Extract milliseconds since UNIX Epoch from ULID
decodeTime("01ARYZ6S41TSV4RRFFQ69G5FAV"); // 1469918176385

Validate ULID

Import isValid to check if a string is a valid ULID:

import { isValid } from "ulidx";

isValid("01ARYZ6S41TSV4RRFFQ69G5FAV"); // true
isValid("01ARYZ6S41TSV4RRFFQ69G5FA"); // false

Crockford's Base32 (Typos tolerance and Hyphened ULIDs)

Import fixULIDBase32 to fix typos and remove hyphens in a ULID:

import { fixULIDBase32 } from "ulidx";

fixULIDBase32("oLARYZ6-S41TSV4RRF-FQ69G5FAV"); // 01ARYZ6S41TSV4RRFFQ69G5FAV

Pseudo-Random Number Generation (PRNG)

ulidx will attempt to locate a suitable cryptographically-secure random number generator in the environment where it's loaded. On NodeJS this will be crypto.randomBytes and in the browser it will be crypto.getRandomValues.

Math.random() is not supported: The environment must have a suitable crypto random number generator.

Compatibility

ulidx is compatible with the following environments:

  • NodeJS 10 and up
    • Node REPL
  • Browsers with working crypto / msCrypto libraries
    • Web workers
  • React-Native ¹

ulidx is not compatible with Cloudflare Workers due to their problematic stance on getting the current time.

¹ React-Native is supported if crypto.getRandomValues() is polyfilled. react-native-get-random-values is one such library that should work well with ulidx. It should be imported before ulidx is used.

Web

ulidx is not currently bundled for web - you can do this yourself using a tool like Webpack or Rollup. You should absolutely disable polyfills for crypto in this case, as ulidx will use the built-in crypto global API rather than any polyfilled crypto anyway. Including a polyfill for crypto will just bloat your application.

ulidx's People

Contributors

perry-mitchell avatar bluet avatar grempe avatar cadienvan avatar

Stargazers

Roman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.