JSPM

  • Created
  • Published
  • Downloads 696971
  • Score
    100M100P100Q194708F
  • License MIT

Generate random or sequential UUID of any length

Package Exports

  • short-unique-id

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (short-unique-id) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Short Unique ID (UUID) Generating Library

Try short-unique-id on RunKit NPM Downloads

(Try out the online generator)

With ES6 imports, Node.js, and browser support

This started as a straight up manual transpilation to ES6 of the short-uid npm package by Ankit Kuwadekar.

image depicting over 5000 weekly npm downloads

Since my package is now reporting between 4k and 5k+ npm weekly downloads, I've gone ahead and refactored the package using Neutrino JS.

V2.x

Version 2.0 was mainly a refactor of the original branch, so functionality and naming conventions have been kept the same.

Even so, there are still breaking changes, mainly the removal of the lib build output, as well as changes on how to instantiate the library on the browser.

Instantiation (Server-side)

Install:

yarn add short-unique-id

ES6:

// Import
import ShortUniqueId from 'short-unique-id';

// Instantiate
const uid = new ShortUniqueId();

Node.js:

// Import
const ShortUniqueId = require('short-unique-id').default;

// Instantiate
const uid = new ShortUniqueId();

Instantiation (Client-side)

Browser:

<!-- Import -->
<script src="https://rawgit.com/jeanlescure/short-unique-id/master/dist/short-unique-id.min.js"></script>

<!-- Instantiate -->
<script>
  var ShortUniqueId = window['short-unique-id'].default;
  var uid = new ShortUniqueId({debug: true});
</script>

Options

There are three options available on instantiation:

const options = {
  dictionary: ['Z', 'a', 'p', 'h', 'o', 'd' ...], // User-defined dictionary
  skipShuffle: false, // If true, sequentialUUID will iterate over the dictionary in the given order
  debug: false, // If true the instance will console.log useful info
};

Usage

Once instantiated you can use one of two functions:

// Generate Random Unique ID of a specific length
uid.randomUUID(6); // zUvMF8
uid.randomUUID(8); // 4308OPWZ
uid.randomUUID(13); // o0Sf6rfoPOrz5

// Generate Sequential Unique ID based on internal dictionary and counter
uid.sequentialUUID(); // v
uid.sequentialUUID(); // 0
uid.sequentialUUID(); // Y

Development

Tests run using Jasmine:

yarn test

Using airbnb rules for eslint:

yarn lint

Build

In order to publish the latest changes you must build the distribution files:

yarn build
yarn dist:update

This will generate the short-unique-id.min.js file under the ./dist directory.