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
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:
📣 v4 Notice
BREAKING CHANGES
This project has been completely refactored from v3 to be less "Deno centric" and more good 'ol Typescript.
We tried avoiding breaking changes, and generally succeded.
With that said, for Node.js require use-cases:
- The
dist
js files are now generated as UMD, named as ShortUniqueId
, which means that...
const { default: ShortUniqueId } = require('short-unique-id');
const ShortUniqueId = require('short-unique-id');
- The
lib
directory is now completely removed
Also, the following changes might generate errors in some edge-cases:
- The
short_uuid
Deno submodule has been completely removed from this repo and now all the logic lives in src/index.ts
- Typings are no longer under the
typings
directory but are now under the dist
directory - A sourcemap is now included along with the
dist
files
New Features 🥳
We have added the ability to generate UUIDs that contain a timestamp which can be extracted:
const uid = new ShortUniqueId();
const uidWithTimestamp = uid.stamp(32);
console.log(uidWithTimestamp);
console.log(uid.parseStamp(uidWithTimestamp));
Use in CLI
$ npm install -g short-unique-id
$ short-unique-id -h
Use as module
import ShortUniqueId from 'https://cdn.jsdelivr.net/npm/short-unique-id@latest/src/index.ts';
import ShortUniqueId from 'short-unique-id';
const ShortUniqueId = require('short-unique-id');
const uid = new ShortUniqueId();
console.log(uid());
console.log(uid.seq());
Use in browser
<script src="https://cdn.jsdelivr.net/npm/short-unique-id@latest/dist/short-unique-id.min.js"></script>
<script>
var uid = new ShortUniqueId();
document.write(uid());
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.
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.
Since this package is now reporting 12k+ npm weekly downloads, we've gone ahead and re-written the whole of it in TypeScript and made sure to package dist modules compatible with Deno, Node.js and all major Browsers.
Development
Clone this repo:
git clone git@github.com:jeanlescure/short-unique-id.git
git clone https://github.com/jeanlescure/short-unique-id.git
Tests run using:
yarn test
Build
In order to publish the latest changes you must build the distribution files:
yarn build
Then commit all changes and run the release script:
yarn release
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-2021 Short Unique ID Contributors.
Licensed under the Apache License 2.0.