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 Generator
With ES6 imports, Node.js, and browser support
This is a straight up manual transpilation to ES6 of the short-uid npm package by Ankit Kuwadekar.
Why?
The above linked repository has gone stale, no updates in the past 2 years (as of writing this README).
If used in a ES6 environment it fails to follow good import
standards.
Even so, the functionality is still useful. So here we are.
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 uid = new ShortUniqueId();
</script>
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:
npm run lint
Build
In order to publish the latest changes you must build the distribution files:
npm run build
This will update the short-unique-id.js
file under ./lib
, which will then be minified as the short-unique-id.min.js
file under the ./dist
directory.