What is short-uuid?
The short-uuid npm package is a utility for generating and working with short, URL-friendly UUIDs. It provides a way to encode and decode UUIDs into shorter formats, making them more suitable for use in URLs, database keys, and other contexts where a shorter identifier is beneficial.
What are short-uuid's main functionalities?
Generate Short UUID
This feature allows you to generate a new short UUID. The `short()` function creates a new translator instance, and `translator.new()` generates a new short UUID.
const short = require('short-uuid');
const translator = short();
const shortUUID = translator.new();
console.log(shortUUID);
Translate UUID to Short UUID
This feature allows you to convert a standard UUID to a short UUID. The `translator.fromUUID(uuid)` method takes a standard UUID and returns its shorter version.
const short = require('short-uuid');
const translator = short();
const uuid = '123e4567-e89b-12d3-a456-426614174000';
const shortUUID = translator.fromUUID(uuid);
console.log(shortUUID);
Translate Short UUID to UUID
This feature allows you to convert a short UUID back to a standard UUID. The `translator.toUUID(shortUUID)` method takes a short UUID and returns the original standard UUID.
const short = require('short-uuid');
const translator = short();
const shortUUID = 'SOME_SHORT_UUID';
const uuid = translator.toUUID(shortUUID);
console.log(uuid);
Custom Alphabet
This feature allows you to create a translator with a custom alphabet. The `short('0123456789abcdef')` function creates a new translator instance that uses the specified alphabet for encoding.
const short = require('short-uuid');
const customTranslator = short('0123456789abcdef');
const shortUUID = customTranslator.new();
console.log(shortUUID);
Other packages similar to short-uuid
uuid
The uuid package is a popular library for generating RFC4122 UUIDs. Unlike short-uuid, it does not provide functionality for shortening UUIDs, but it is widely used for generating standard UUIDs in various versions (v1, v3, v4, v5).
nanoid
Nanoid is a small, secure, URL-friendly unique string ID generator. It is similar to short-uuid in that it generates shorter IDs suitable for URLs, but it does not focus on translating standard UUIDs to shorter formats and vice versa.
cuid
Cuid is a collision-resistant ID generator optimized for horizontal scaling and performance. It generates short, readable IDs that are URL-friendly, similar to short-uuid, but it does not provide translation between standard UUIDs and short UUIDs.
short-uuid
Generate and translate standard UUIDs into shorter - or just different - formats and back.
v5.2.0
5.2.0 adds validate
method to check short IDs. Requested by @U-4-E-A
5.1.0 adds translation support for the uuid25 (Base36) format
with the uuid25Base36
constant.
Major Changes in 5.0.0
- 🛑 5.0.0 drops support for Node 10 and 12.
Quick Start
const short = require('short-uuid');
short.generate();
Details
short-uuid starts with RFC4122 v4-compliant UUIDs and translates them
into other, usually shorter formats. It also provides translators
to convert back and forth from RFC compliant UUIDs to the shorter formats,
and validate the IDs.
As of 4.0.0, formats return consistent-length values unless specifically requested.
This is done by padding the start with the first ([0]
) character in the alphabet.
Previous versions can translate padded formats back to UUID.
const short = require('short-uuid');
const shortId = short.generate();
const translator = short();
const decimalTranslator = short("0123456789");
const cookieTranslator = short(short.constants.cookieBase90);
translator.new();
translator.generate();
translator.toUUID(shortId);
translator.fromUUID(regularUUID);
translator.validate(shortId);
translator.validate(shortId, true);
translator.validate('0000000000000000000000', true)
translator.alphabet;
translator.maxLength;
short.constants.cookieBase90;
short.constants.flickrBase58;
short.constants.uuid25Base36;
short.uuid();
translator.uuid();
Options
short-uuid 4.0.0 added support for options when creating a translator.
This may support additional configuration in the future.
const short = require('short-uuid');
const translator = short(short.constants.flickrBase58, {
consistentLength: false,
});
translator.new();
consistentLength
- Controls padding on shortened values. Default is true
.
Support
short-uuid 5.x
and later is tested on Node 14.x and later.
short-uuid 4.x
was tested on Node 8.x to 18.x
short-uuid 3.x
and lower was confirmed to work on Node 6.x to 12.x,
and offered a precompiled browser library proposed
by voronianski.
Notes
short-uuid provides RFC4122 v4-compliant UUIDs,
thanks to uuid
.
TypeScript definitions are included, thanks to
alexturek.
Previous Release Note Highlights
5.0.0 drops support for Node 12 and below.
4.1.0 adds a maxLength value to translators for reference
4.0.1 adds consistent length translation and throws an error if provided an invalid alphabet.
3.1.1 removed Node 4.x tests. Last included Browserify distribution.
2.3.4 corrects the behavior for UUIDs with uppercase letters. Last version to build on Node 0.x.
Please see Revisions for information on previous versions.