UniqueId is a lightweight TypeScript utility for generating unique identifiers.
While there are numerous libraries available for generating unique identifiers, each with its own set of features and use cases, UniqueId focuses on providing a straightforward, easy-to-use solution for common database operations. It's designed to be:
- Simple: Generate IDs with minimal code and setup.
- Flexible: Customize ID length and add prefixes as needed.
- Sufficiently Random: Uses character shuffling for good distribution.
- Practical: Tailored for everyday database tasks without unnecessary complexity.
UniqueId doesn't aim to be a one-size-fits-all solution. Instead, it offers a focused set of features that cater to specific, common requirements in database management and application development.
If you're looking for a no-frills, efficient way to generate unique identifiers for your projects, UniqueId might be just what you need.
UniqueId can be used in different JavaScript environments. Here's how to install and import it:
If you're using Deno or a runtime that supports JSR imports, you can import UniqueId directly:
import { UniqueId } from "jsr:@dnl-fm/unique-id";
No additional installation steps are required for JSR imports.
For Node.js or environments using npm, first install the package:
npx jsr add @dnl-fm/money-ts
Then, import it in your project:
import { UniqueId } from "@dnl-fm/unique-id";
Here are some examples of how to use the UniqueId class:
To generate a default ID (26 characters, no prefix):
const id = UniqueId.create();
console.log(id); // Output: "7x3a9b2m1p5q8r4c6d0f2g3h7j"
To generate an ID with a custom length:
const shortId = UniqueId.create({ length: 10 });
console.log(shortId); // Output: "a1b2c3d4e5"
To generate an ID with a custom prefix:
const userId = UniqueId.create({ prefix: "user_" });
console.log(userId); // Output: "user_7x3a9b2m1p5q8r4c6d0f2g"
To generate an ID with both custom length and prefix:
const itemId = UniqueId.create({ length: 20, prefix: "item_" });
console.log(itemId); // Output: "item_3a9b2m1p5q8r4c6d0"
Creates a unique identifier.
options.length
(optional): The total length of the ID (including prefix). Defaults to 26.options.prefix
(optional): A string to prepend to the random part of the ID. Defaults to an empty string.
Returns a string containing the unique identifier.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.