shortid
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
nanoid | 2.1.11 | 9.95 kB | MIT | prod |
Shortid is an intrusive, non-sequential, and URL-friendly unique id generator optimized for Node.js. It promises to create IDs that are perfect for URL shorteners, MongoDB and Redis IDs, as well as any other ID users might encounter. The URI-friendly IDs generated usually consist of 7-14 characters, including the ranges 'A-Z', 'a-z', '0-9', and '_-'. While its architecture may not be fit for generating cryptographically secure IDs, it does an excellent job in most use cases, namely when creating game IDs, where predictability would foster cheating.
To use Shortid in a JavaScript application, you simply import it using Node's require function, after which you can easily call the generate
function to produce new unique IDs. Below are examples on how to use it:
// Importing shortid
const shortid = require('shortid');
// Generating a new id
console.log(shortid.generate()); // Outputs a short non-sequential url-friendly unique id e.g 'PPBqWA9'
Also, you can use it in conjunction with a popular ODM (Object Document Mapper) known as Mongoose to generate unique IDs for your documents like so:
_id: {
'type': String,
'default': shortid.generate
},
Further still, you can customize the characters used by Shortid by calling the characters
function with a 64 unique character string parameter. For example:
shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
The Shortid documentation can be found directly on the package's GitHub page at https://github.com/dylang/shortid. You'll find a comprehensive rundown of the API and its functions, which will provide all you need to maximize the utility of the Shortid package.