This site uses cookies to provide a high quality user experience and gather anonymized data for statistical analysis of the website's traffic. You can learn more by reading our cookie policy.

To consent, continue navigating this site or click the "Accept cookies" button.

Options
All
  • Public
  • Public/Protected
  • All
Menu

short-unique-id - v3.2.3

Short Unique ID

image depicting order and disorder

This page is part of the open source javascript library short-unique-id.

For usage examples click here.

Online (short) UUID generator

Press the buttons below to generate your short uuid codes:

Length:

Your random UUID:  

Your sequential UUID:  

Random Color generator

The ability to set a custom dictionary and length means that Short Unique ID is useful for many other cases such as this random color generator.

    
      var uid = new ShortUniqueId({
        dictionary: [
          '0', '1', '2', '3',
          '4', '5', '6', '7',
          '8', '9', 'A', 'B',
          'C', 'D', 'E', 'F',
        ],
      });
      var color = '#' + uid.randomUUID(6) + ';';
    
  

Your random color:  

Short Unique ID (UUID) Generating Library

Tests Try short-unique-id on RunKit NPM Downloads JsDelivr Hits Deno v1.2.0

9 Contributors

Generate random or sequential UUID of any length.

This project is open to updates by its users, I ensure that PRs are relevant to the community. In other words, if you find a bug or want a new feature, please help us by becoming one of the contributors ✌️ ! See the contributing section.

Like this module? ❤

Please consider:

Use in CLI 🆕

$ npm install -g short-unique-id

$ short-unique-id -h

# Usage:
#   node short-unique-id [OPTION]

# Options:
#   -l, --length=ARG         character length of the uid to generate.
#       --dictionaryJson=ARG json file with dictionary array.
#   -h, --help               display this help

Use as module

// Deno (web module) Import
import ShortUniqueId from 'https://cdn.jsdelivr.net/npm/short-unique-id@latest/short_uuid/mod.ts';

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

//or Node.js require
const { default: ShortUniqueId } = require('short-unique-id');

//Instantiate
const uid = new ShortUniqueId();

// Random UUID
console.log(uid());

// Sequential UUID
console.log(uid.seq());

Use in browser

<!-- Import -->
<script src="https://cdn.jsdelivr.net/npm/short-unique-id@latest/dist/short-unique-id.min.js"></script>

<!-- Usage -->
<script>
  // Instantiate
  var uid = new ShortUniqueId();

  // Random UUID
  document.write(uid());

  // Sequential UUID
  document.write(uid.seq());
</script>

Options

Options can be passed when instantiating uid:

const options = { ... };

const uid = new ShortUniqueId(options);

For more information take a look at the docs.

Available for

Documentation with Online Short UUID Generator

You can find the docs and online generator at:

https://shortunique.id

What is the probability of generating the same id again?

This largely depends on the given dictionary and the selected UUID length.

Out of the box this library provides a shuffled dictionary of digits from 0 to 9, as well as the alphabet from a to z both in UPPER and lower case, with a default UUID length of 6. That gives you a total of 56,800,235,584 possible UUIDs.

So, given the previous values, the probability of generating a duplicate in 1,000,000 rounds is ~0.00000002, or about 1 in 50,000,000.

If you change the dictionary and/or the UUID length then we have provided the function collisionProbability() function to calculate the probability of hitting a duplicate in a given number of rounds (a collision) and the function uniqueness() which provides a score (from 0 to 1) to rate the "quality" of the combination of given dictionary and UUID length (the closer to 1, higher the uniqueness and thus better the quality).

To find out more about the math behind these functions please refer to the API Reference.

V3.x

Version 3.0 is a re-write of this library aiming to use Deno as the driving force of the development and distribution of Short Unique ID.

We did this by separating the Deno compatible typescript logic into a git submodule which gets bundled for the rest of the platforms by the scripts within the package.json in this repo.

Another reason for the re-write was to define a new license for the source code within this repo, the Apache 2.0 license.

"Deno" who? what?

Ryan Dahl, creator of Node.js, has spent the last couple of years working on Deno, a new runtime for JavaScript that is supposed to fix all the inherent problems of Node.

We have tried it out and are convinced that Deno is stable enough and delivers on all expectations we could have.

For more info check out this detailed look at Deno from LogRocket's blog.

Acknowledgement and platform support

This repo and npm package 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 this package is now reporting between 4k and 5k+ npm weekly downloads, we've gone ahead and re-written the whole of it in TypeScript and made sure to package lib and dist modules compatible with Deno, Node.js and all major Browsers.

Development

Clone this repo (including submodules):

# SSH
git clone --recurse-submodules git@github.com:jeanlescure/short-unique-id.git

# HTTPS
git clone --recurse-submodules https://github.com/jeanlescure/short-unique-id.git

All feature development must happen under ./short_uuid/mod.ts.

Tests run using Deno:

yarn test

Using airbnb rules for tslint:

yarn lint

Build

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

yarn lib:build
yarn dist:build

IMPORTANT: The dist:build script depends on the files generated by lib:build.

Contributing

Yes, thank you! This plugin is community-driven, most of its features are from different authors. Please update the docs and tests and add your name to the package.json file.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

💻
🚧 💻 🖋 📖 ⚠️
💻
💻
💻 👀
💻
💻
🤔
🛡️

License

Copyright (c) 2018-2020 Short Unique ID Contributors.
Licensed under the Apache License 2.0.