What is short-unique-id?
The short-unique-id npm package is a utility for generating short, unique, non-sequential ids. It is useful for creating unique identifiers for various purposes such as database keys, session tokens, and more.
What are short-unique-id's main functionalities?
Generate a unique ID
This feature allows you to generate a unique ID with the default settings. The generated ID is a short, unique string.
const ShortUniqueId = require('short-unique-id');
const uid = new ShortUniqueId();
console.log(uid());
Custom length of the ID
This feature allows you to specify the length of the generated ID. In this example, the ID will be 10 characters long.
const ShortUniqueId = require('short-unique-id');
const uid = new ShortUniqueId({ length: 10 });
console.log(uid());
Custom dictionary for ID generation
This feature allows you to use a custom dictionary for generating the ID. In this example, the ID will be generated using only hexadecimal characters.
const ShortUniqueId = require('short-unique-id');
const uid = new ShortUniqueId({ dictionary: '0123456789ABCDEF' });
console.log(uid());
Generate multiple unique IDs
This feature allows you to generate multiple unique IDs at once. In this example, an array of 5 unique IDs is generated.
const ShortUniqueId = require('short-unique-id');
const uid = new ShortUniqueId();
const ids = Array.from({ length: 5 }, () => uid());
console.log(ids);
Other packages similar to short-unique-id
uuid
The uuid package is a popular library for generating UUIDs (Universally Unique Identifiers). Unlike short-unique-id, which generates shorter IDs, uuid generates longer, more complex IDs that are globally unique. It is commonly used for creating unique identifiers in distributed systems.
nanoid
The nanoid package is a small, secure, URL-friendly unique string ID generator. It is similar to short-unique-id in that it generates short, unique IDs, but it focuses on security and URL-friendliness. Nanoid is also highly customizable in terms of ID length and character set.
cuid
The cuid package is designed for generating collision-resistant IDs optimized for horizontal scaling and performance. It generates longer IDs compared to short-unique-id and includes features like timestamp and counter to ensure uniqueness even in distributed environments.
Short Unique ID (UUID) Generating Library
(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.
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 using npm:
npm install --save short-unique-id
ES6:
import ShortUniqueId from 'short-unique-id';
const uid = new ShortUniqueId();
Node.js:
var ShortUniqueId = require('short-unique-id');
var uid = new ShortUniqueId();
Instantiation (Client-side)
Browser:
<script src="https://rawgit.com/jeanlescure/short-unique-id/master/dist/short-unique-id.min.js"></script>
<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' ...],
skipShuffle: false,
debug: false,
};
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:
npm test
This will update the short-unique-id.js
file under ./lib
, which will then be imported by the spec and tested.
Find lint using airbnb 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.