8000 GitHub - dnl-fm/unique-id: Utility class for generating unique identifiers
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dnl-fm/unique-id

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UniqueId: Simple, Flexible ID Generation

UniqueId is a lightweight TypeScript utility for generating unique identifiers.

Why Another ID Generator?

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:

  1. Simple: Generate IDs with minimal code and setup.
  2. Flexible: Customize ID length and add prefixes as needed.
  3. Sufficiently Random: Uses character shuffling for good distribution.
  4. 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.

Installation

UniqueId can be used in different JavaScript environments. Here's how to install and import it:

Using JSR

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.

Using npm

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

Usage

Here are some examples of how to use the UniqueId class:

Basic Usage

To generate a default ID (26 characters, no prefix):

const id = UniqueId.create();
console.log(id); // Output: "7x3a9b2m1p5q8r4c6d0f2g3h7j"

Custom Length

To generate an ID with a custom length:

const shortId = UniqueId.create({ length: 10 });
console.log(shortId); // Output: "a1b2c3d4e5"

Custom Prefix

To generate an ID with a custom prefix:

const userId = UniqueId.create({ prefix: "user_" });
console.log(userId); // Output: "user_7x3a9b2m1p5q8r4c6d0f2g"

Custom Length and Prefix

To generate an ID with both custom length and prefix:

const itemId = UniqueId.create({ length: 20, prefix: "item_" });
console.log(itemId); // Output: "item_3a9b2m1p5q8r4c6d0"

API Reference

UniqueId.create(options?: { length?: number; prefix?: string }): string

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.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is open source and available under the MIT License.

About

Utility class for generating unique identifiers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0