npm.io
2.0.5 • Published 4 months ago

generate-unique-id

Licence
MIT
Version
2.0.5
Deps
0
Size
9 kB
Vulns
0
Weekly
0

Generate Unique ID

A lightweight JavaScript utility for generating random ID strings with customizable length, character sets, and symbol inclusion/exclusion rules.

Perfect for generating:

  • Temporary identifiers
  • Form element IDs
  • Test data
  • Reference codes
  • Client-side unique keys

Try the interactive demo

Installation

Using npm:

npm i generate-unique-id

Using yarn:

yarn add generate-unique-id

Usage

CommonJS
const generateUniqueId = require('generate-unique-id'); 

const id = generateUniqueId(); 

console.log(id); 
// Example output: "8p7m2k1v9q4t5r6s3x0a"
// 20 characters, letters and numbers
ES Modules
import generateUniqueId from 'generate-unique-id'; 

const id = generateUniqueId();

console.log(id)
// Example output: "i0t58pe1iieiqsz6htd0"
// 20 characters, letters and numbers
Examples
// Generate a longer ID
const id1 = generateUniqueId({ 
  length: 32 
}); 
console.log(id1); // "j4w0g8n3x6p9t2v7r5k1m8q4c0y6h2z"

// Numbers only
const id2 = generateUniqueId({ 
  useLetters: false 
}); 
console.log(id2); // "58027491362018475329"

// Add custom symbols
const id3 = generateUniqueId({ 
  includeSymbols: ['@', '#', '|'] 
}); 
console.log(id3); // "g7@r3m#x9|k2v8a"

// Exclude specific characters
const id4 = generateUniqueId({ 
  excludeSymbols: ['0', 'O', '1', 'l'] 
}); 
console.log(id4); // "bxf7qaZ9yprtmv"

Options

Option Type Default Description
length number 20 Length of the generated ID.
useLetters boolean true Include letters (a-z) in the character pool.
useNumbers boolean true Include numbers (0-9) in the character pool.
includeSymbols array [] Additional characters to include in the character pool.
excludeSymbols array [] Characters to remove from the character pool.

Why not UUID?

UUIDs are great for globally unique identifiers, but they are often longer and less readable.

// UUID
550e8400-e29b-41d4-a716-446655440000

// generate-unique-id
x8k2m7q4v9r5t1p6

Use generate-unique-id when you need:

  • Shorter IDs
  • Custom lengths
  • Numbers-only IDs
  • Custom character sets
  • Human-friendly identifiers

Notes

  • Generated IDs are random and intended for general-purpose identification
  • This package does not generate RFC-compliant UUIDs
  • The probability of collisions is very low for typical use cases, but uniqueness is not mathematically guaranteed

License

MIT license

Keywords