Short but unique IDs.
- Short but unique IDs
- The possibility of collision is impossible
- Suitable for distributed systems
- Suitable for sorting and database indexes
- Snowflake IDs developed by Twitter (X) can be generated
- Symbolic IDs can be generated like YouTube's video IDs
You can install it as follows.
// NPM
npm install uuniq
// PNPM
pnpm install uuniq
// Yarn
yarn add uuniq
// Bun
bun add uuniq
// Deno
deno install npm:uuniq
Briefly as follows.
TypeScript
import { Snowflake, Symbolic, type Types as UuniqTypes } from "uuniq";JavaScript
import { Snowflake, Symbolic } from "uuniq";
new Snowflake(options?)
Snowflake IDs developed by Twitter (X) in 2010. Unique IDs can be generated in distributed systems by specifying Place IDs.
Parameter Default Description options SnowflakeOptions (optional)
Constructor's options.options.epoch "2025-01-01T00:00:00.000Z"
String | Number | Date (optional)
Date of epoch.options.place_id 0
Number (optional)
Place ID for distributed systems.Example:
const SnowflakeIDs: Snowflake = new Snowflake({ epoch: "2025-01-01T00:00:00.000Z", place_id: 0 });
new Symbolic(options?)
Unique IDs like YouTube's video IDs. Unique IDs can be generated in distributed systems by specifying Place IDs.
Parameter Default Description options SymbolicOptions (optional)
Constructor's options.options.epoch "2025-01-01T00:00:00.000Z"
String | Number | Date (optional)
Date of epoch.options.place_id 0
Number (optional)
Place ID for distributed systems.options.charset "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
String (optional)
Character set of IDs.Example:
const SymbolicIDs: Symbolic = new Symbolic({ epoch: "2025-01-01T00:00:00.000Z", place_id: 0, charset: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" });
Snowflake.generate()
Generate Snowflake IDs developed by Twitter (X) in 2010.
Parameter Default Description returns String
Example:
const id_0: string = SnowflakeIDs.generate(); // "102604921389056" const id_1: string = SnowflakeIDs.generate(); // "102604921389057"
Snowflake.resolve(id)
Resolve the previously created ID. For this, the epoch
and place_id
values in the Constructor must be correct.
Parameter Default Description id String
ID to be resolved.returns SnowflakeResolve
Example:
const resolve: UuniqTypes.SnowflakeResolve = SnowflakeIDs.resolve("102604921389056"); /* { created_at: "2025-03-14T11:35:07.409Z", place_id: 0, sequence: 0 } */
Symbolic.generate()
Generate unique IDs like YouTube's video IDs.
Parameter Default Description returns String
Example:
const id_0: string = SymbolicIDs.generate(); // "T8Qu56ki" const id_1: string = SymbolicIDs.generate(); // "T8Qu56kj"
Symbolic.resolve(id)
Resolve the previously created ID. For this, the epoch
and place_id
values in the Constructor must be correct.
Parameter Default Description id String
ID to be resolved.returns SymbolicResolve
Example:
const resolve: UuniqTypes.SymbolicResolve = SymbolicIDs.resolve("T8Qu56ki"); /* { created_at: "2025-03-14T11:36:05.528Z", place_id: 0, sequence: 0 } */
Type Place SnowflakeOptions new Snowflake(options?) SnowflakeResolve Snowflake.resolve(id) SymbolicOptions new Symbolic(options?) SymbolicResolve Symbolic.resolve(id) Example:
import { Snowflake, Symbolic, type Types as UuniqTypes } from "uuniq"; const SnowflakeOptions: UuniqTypes.SnowflakeOptions = { epoch: "2025-01-01T00:00:00.000Z", place_id: 0 }; const SymbolicOptions: UuniqTypes.SymbolicOptions = { epoch: "2025-01-01T00:00:00.000Z", place_id: 0, charset: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" }; const SnowflakeIDs: Snowflake = new Snowflake(SnowflakeOptions); const SymbolicIDs: Symbolic = new Symbolic(SymbolicOptions); const snowflake_id: string = SnowflakeIDs.generate(); const symbolic_id: string = SymbolicIDs.generate(); const snowflake_resolve: UuniqTypes.SnowflakeResolve = SnowflakeIDs.resolve(snowflake_id); const symbolic_resolve: UuniqTypes.SymbolicResolve = SymbolicIDs.resolve(symbolic_id);