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.
v3.0.0
short-uuid provides RFC4122 v4-compliant UUIDs,
thanks to uuid
.
3.0.0 drops support for Node 0.10 and 0.12, as nested dependencies were updated and it breaks the build.
There are no functional changes in 3.0.0 from 2.3.4.
It includes Browserify support for client-side use as proposed by voronianski,
with compiled browser-ready files in the npm package for convenience. The library is exposed as ShortUUID
.
TypeScript definitions are included, thanks to alexturek.
var short = require('short-uuid');
var translator = short();
var decimalTranslator = short("0123456789");
var cookieTranslator = short(short.constants.cookieBase90);
translator.new();
short.uuid();
translator.uuid();
translator.toUUID(shortId);
translator.fromUUID(regularUUID);
translator.alphabet;
short.constants.flickrBase58;
short.constants.cookieBase90;
short-uuid is under 1K when compressed. Using Browserify, the library and dependencies are ~3.5K.
Recent Release Notes
2.3.3 fixes missing /dist folder from the npm module.
2.3.4 corrects the behavior for UUIDs with uppercase letters.
3.0.0 updates dependencies, includes a refactor for CodeClimate, and drops Node 0.x support.
Prior to 2.3.4, passing a UUID with capital letters would cause an incorrect conversion.
All UUIDs are now converted to lowercase before translation.
UUIDs generated by the uuid
library were always lowercase. This was a test case miss.
Please see Revisions for information on previous versions.