A lightweight TypeScript utility for generating various types of unique identifiers. This library provides three different ID generation methods, each optimized for specific use cases.
import { id } from '@its-satyajit/id-generator'; // Generate different types of IDs const id = id.generate(); // "Zk9mP2nQ4R" const id20 = id.generate(20); // "2sgU035ser58knde42at" const shortId = id.shortUuidV7(); // "7LJSr5nwRp9vK3mX2yD4qN" const shortId = id.uuidV7()(); // "f47d08e7-9f9d-7a2e-b290-5a96f0b8c9e1" const timeId = id.timeId(); // "lq1pgsk-x7y8z9w0"
id.generate()
Generates a 16-character random identifier by default.
number
- The length of the ID to generatestring
- A 16 -character random string by default using base62 alphabet (a-zA-Z0-9)"Zk9mP2nQ4R"
id.uuidV7()
Generates a uuid v7 identifier.
string
- A 128 bits containing timestamp and random data"f47d08e7-9f9d-7a2e-b290-5a96f0b8c9e1"
id.shortUuidV7()
Generates a 22-character timestamp-based identifier based on UUID v7 specification.
string
- A 36-character string containing timestamp and random data"7LJSr5nwRp9vK3mX2yD4qN"
id.timeId()
Generates a human-readable time-based identifier.
string
- A time-based ID in format ${timestamp-base36}-${random-base62}
"lq1pgsk-x7y8z9w0"
All methods use the Web Crypto API (crypto.getRandomValues()
) for secure random number generation.
Method | Length | Sortable | Time-Based | Format |
---|---|---|---|---|
generate() |
16/any | No | No | Random |
uuidV7() |
22 | Yes | Yes | Timestamp + Random |
shortUuidV7() |
22 | Yes | Yes | Timestamp + Random |
timeId() |
Variable | Yes | Yes | Timestamp-Random |
generate()
for short, user-facing IDsshortUuidV7()
for database primary keys and distributed systemstimeId()
for logging and debugging purposesWorks in all modern browsers that support the Web Crypto API:
MIT
Add Package
deno add jsr:@its-satyajit/id-generator
Import symbol
import * as id_generator from "@its-satyajit/id-generator";
---- OR ----
Import directly with a jsr specifier
import * as id_generator from "jsr:@its-satyajit/id-generator";
Add Package
npx jsr add @its-satyajit/id-generator
Import symbol
import * as id_generator from "@its-satyajit/id-generator";
Add Package
yarn dlx jsr add @its-satyajit/id-generator
Import symbol
import * as id_generator from "@its-satyajit/id-generator";
Add Package
pnpm dlx jsr add @its-satyajit/id-generator
Import symbol
import * as id_generator from "@its-satyajit/id-generator";
Add Package
bunx jsr add @its-satyajit/id-generator
Import symbol
import * as id_generator from "@its-satyajit/id-generator";