CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-short-uuid

Create and translate standard UUIDs with shorter formats.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Short UUID

Short 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.

Package Information

  • Package Name: short-uuid
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install short-uuid

Core Imports

const short = require('short-uuid');

For ES modules:

import short from 'short-uuid';

Basic Usage

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 check

Architecture

Short UUID is built around three key components:

  • Main Constructor: Creates translator instances with custom alphabets and options
  • Static Methods: Convenience functions for quick UUID generation and access to constants
  • Translator Instance: Provides methods for UUID generation, conversion, and validation with a specific alphabet

Capabilities

Main Constructor Function

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;
}

Static Constants

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"
};

Static UUID Generation

Generate RFC4122 v4-compliant UUIDs without creating a translator.

/**
 * Generate a new RFC4122 v4-compliant UUID
 * @returns {string} Standard UUID with dashes
 */
function uuid();

Static Short UUID Generation

Generate a short UUID using default flickrBase58 encoding.

/**
 * Generate a short UUID using flickrBase58 encoding
 * @returns {string} Shortened UUID
 */
function generate();

Translator Instance Methods

The translator instance returned by the main constructor provides these methods:

Generate Short UUIDs

/**
 * 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();

UUID Conversion

/**
 * 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);

Standard UUID Generation

/**
 * Generate a new RFC4122 v4-compliant UUID
 * @returns {string} Standard UUID with dashes
 */
uuid();

Validation

/**
 * 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);

Translator Instance Properties

interface Translator {
  /** The alphabet used for encoding UUIDs */
  alphabet: string;
  /** Maximum length in characters of a short ID using this translator */
  maxLength: number;
}

Usage Examples

Custom Alphabet with Options

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 output

Working with Different Encodings

const 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); // true

Validation Examples

const 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)

Error Handling

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
}

TypeScript Support

// 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;
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/short-uuid@5.2.x
Publish Source
CLI
Badge
tessl/npm-short-uuid badge