Create and translate standard UUIDs with shorter formats.
npx @tessl/cli install tessl/npm-short-uuid@5.2.0Short UUID is a Node.js library that generates and translates RFC4122 v4-compliant UUIDs into shorter, more compact formats and back again. It supports multiple encoding alphabets and provides both simple quick-start functionality and advanced translator creation for custom alphabets.
npm install short-uuidconst short = require('short-uuid');For ES modules:
import short from 'short-uuid';const short = require('short-uuid');
// Quick start - generate a short UUID with default flickrBase58 encoding
const shortId = short.generate(); // '73WakrfVbNJBaAmhQtEeDv'
// Create a custom translator
const translator = short(); // defaults to flickrBase58
const customTranslator = short(short.constants.cookieBase90);
// Generate and translate UUIDs
const shortUuid = translator.generate(); // 'mhvXdrZT4jP5T8vBxuvm75'
const fullUuid = translator.toUUID(shortUuid); // 'a44521d0-0fb8-4ade-8002-3385545c3318'
const backToShort = translator.fromUUID(fullUuid); // 'mhvXdrZT4jP5T8vBxuvm75'
// Validate short UUIDs
translator.validate(shortUuid); // true - format validation
translator.validate(shortUuid, true); // true - format + UUID validity checkShort UUID is built around three key components:
Creates a UUID translator with custom alphabet and options.
/**
* Creates a UUID translator with custom alphabet and options
* @param {string} [toAlphabet] - Custom alphabet for encoding (defaults to flickrBase58)
* @param {Options} [options] - Configuration options
* @returns {Translator} Translator instance with encoding methods
* @throws {Error} Throws error if alphabet contains duplicate characters
*/
function shortUUID(toAlphabet, options);
interface Options {
/** Controls padding on shortened values for consistent length (default: true) */
consistentLength?: boolean;
}Predefined alphabet constants for common encoding schemes.
const constants = {
/** Safe for HTTP cookie values - 90 character alphabet */
cookieBase90: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~",
/** Flickr-style base58 - avoids similar characters (0/O, 1/I/l, etc.) */
flickrBase58: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ",
/** UUID25 standard - 36 character alphabet for 25-character UUIDs */
uuid25Base36: "0123456789abcdefghijklmnopqrstuvwxyz"
};Generate RFC4122 v4-compliant UUIDs without creating a translator.
/**
* Generate a new RFC4122 v4-compliant UUID
* @returns {string} Standard UUID with dashes
*/
function uuid();Generate a short UUID using default flickrBase58 encoding.
/**
* Generate a short UUID using flickrBase58 encoding
* @returns {string} Shortened UUID
*/
function generate();The translator instance returned by the main constructor provides these methods:
/**
* Generate a new short UUID using this translator's alphabet
* @returns {string} Shortened UUID
*/
generate();
/**
* Generate a new short UUID using this translator's alphabet (alias for generate)
* @returns {string} Shortened UUID
*/
new();/**
* Convert standard UUID to short format using this translator's alphabet
* @param {string} regularUUID - Standard UUID with or without dashes
* @returns {string} Shortened UUID
*/
fromUUID(regularUUID);
/**
* Convert short UUID back to standard UUID format
* @param {string} shortId - Shortened UUID
* @returns {string} Standard UUID with dashes
*/
toUUID(shortId);/**
* Generate a new RFC4122 v4-compliant UUID
* @returns {string} Standard UUID with dashes
*/
uuid();/**
* Validate short UUID format and optionally check UUID validity
* @param {string} shortId - The string to check for validity
* @param {boolean} [rigorous=false] - If true, also validates as proper UUID
* @returns {boolean} True if valid according to specified criteria
*/
validate(shortId, rigorous);interface Translator {
/** The alphabet used for encoding UUIDs */
alphabet: string;
/** Maximum length in characters of a short ID using this translator */
maxLength: number;
}const short = require('short-uuid');
// Create translator with variable length output (like v3.x behavior)
const variableLength = short(short.constants.flickrBase58, {
consistentLength: false
});
const shortId = variableLength.generate(); // Variable length outputconst short = require('short-uuid');
// Create translators for different encodings
const cookieTranslator = short(short.constants.cookieBase90);
const flickrTranslator = short(short.constants.flickrBase58);
const uuid25Translator = short(short.constants.uuid25Base36);
// Same UUID in different encodings
const originalUuid = short.uuid();
const cookieFormat = cookieTranslator.fromUUID(originalUuid);
const flickrFormat = flickrTranslator.fromUUID(originalUuid);
const uuid25Format = uuid25Translator.fromUUID(originalUuid);
// All translate back to the same UUID
console.log(cookieTranslator.toUUID(cookieFormat) === originalUuid); // true
console.log(flickrTranslator.toUUID(flickrFormat) === originalUuid); // true
console.log(uuid25Translator.toUUID(uuid25Format) === originalUuid); // trueconst translator = short();
// Format validation only (checks length and alphabet)
translator.validate('mhvXdrZT4jP5T8vBxuvm75'); // true
translator.validate('invalid-chars-!@#'); // false (invalid characters)
translator.validate('short'); // false (wrong length)
// Rigorous validation (format + UUID validity)
translator.validate('mhvXdrZT4jP5T8vBxuvm75', true); // true
translator.validate('1111111111111111111111', true); // false (valid format but invalid UUID)const short = require('short-uuid');
// Invalid alphabet with duplicate characters throws an error
try {
const invalidTranslator = short('001234567899aabcdef'); // Contains duplicates
} catch (error) {
console.error('Invalid alphabet:', error.message);
// Error: The provided Alphabet has duplicate characters resulting in unreliable results
}// Type definitions - branded types for better type safety
type UUID = string & { _guidBrand: 'uuid' }; // Standard UUID format
type SUUID = string & { _guidBrand: 'short-uuid' }; // Shortened UUID format
interface Options {
consistentLength?: boolean;
}
interface Translator {
alphabet: string;
maxLength: number;
new(): SUUID;
generate(): SUUID;
uuid(): UUID;
toUUID(shortId: string | SUUID): UUID;
fromUUID(regularUUID: string | UUID): SUUID;
validate(shortId: string | SUUID, rigorous?: boolean): boolean;
}
// Main function signature
declare function shortUUID(alphabet?: string, options?: Options): Translator;
// Static properties
declare namespace shortUUID {
const constants: {
cookieBase90: string;
flickrBase58: string;
uuid25Base36: string;
};
function uuid(): UUID;
function generate(): SUUID;
}