genid-pro
TypeScript icon, indicating that this package has built-in type declarations

1.0.15 • Public • Published

genIdPro

genIdPro is a Node.js library designed for generating unique and customizable IDs with various options. It combines high entropy sources like the system's process ID and machine ID with a custom random generation mechanism to ensure uniqueness and robustness. This library provides multiple methods to generate IDs, including time-based, UUID-style, and base encoding formats. Additionally, it includes features like ID expiration, customizable separators, and dynamic prefix/suffix generation.

Features

1. Generate Unique IDs

  • The generateUnique() method ensures the uniqueness of the generated ID by checking previously generated IDs.
  • It guarantees that no two generated IDs are the same.

2. Customizable Length & Format

  • You can specify the length of the generated ID.
  • The format can be customized with prefixes, suffixes, and separators.

3. Time-Based ID Generation

  • The generateTimeBasedId() method generates IDs based on the current timestamp, ensuring a time-ordered sequence of IDs.

4. Base Encoding

  • The generateBaseEncoding() method allows you to generate IDs in different encoding formats (Base-36, Base-64, etc.).
  • You can adjust the base encoding format for the ID string generation.

5. UUID-Style IDs

  • The generateUUID() method generates IDs similar to UUID format, using the current timestamp and random entropy.

6. ID Expiration (TTL)

  • The generateWithExpiration() method generates IDs with an expiration time (Time-To-Live) defined in minutes.
  • The expiration date is embedded in the ID, allowing you to track the validity period.

7. Custom Separator

  • The generateWithCustomSeparator() method allows you to customize the separator used between different parts of the ID (e.g., timestamp, process ID, etc.).

8. Dynamic Prefix/Suffix

  • The generateWithDynamicPrefixSuffix() method adds dynamic prefixes and suffixes based on the timestamp and random part of the ID.
  • This provides an extra layer of randomness and uniqueness.

9. ID Decoding

  • The decode() method allows you to decode the generated ID into its original components (timestamp, process ID, machine ID, and random part).
  • This is useful for parsing and analyzing the ID.

10. ID Length Validation

  • The validateIdLength() method ensures the generated ID does not exceed a specified length.

11. Change ID Structure

  • The change() method allows you to modify an existing ID by changing its prefix, suffix, or length, simulating an update or restructuring of the ID.

12. Machine ID & Process ID Generation

  • The library generates unique machine and process identifiers using the system's MAC address (or hostname as fallback) and the process ID.

13. Customizable Project Name in ID Generation

  • The generateProjectId() method allows users to add a customizable project name to their IDs.
  • A maximum of 7 characters from the project name will be included to maintain length consistency.

Installation

To install genIdPro in your project, run the following command:

npm install genid-pro

Usage

  1. Import the Library

    const genIdPro = require('genid-pro');
  2. Initialize the Library Call init() to initialize the machine and process IDs:

    genIdPro.init();
  3. Generate Unique IDs Generate a unique ID with a customizable prefix, suffix, and length:

    const uniqueId = genIdPro.generateUnique('', '', 18);
    console.log(uniqueId);
  4. Generate Time-Based IDs Generate an ID based on the current timestamp:

    const timestampId = genIdPro.generateTimeBasedId('', '', 18);
    console.log(timestampId);
  5. Generate Base Encoding IDs Generate an ID in Base-36 encoding:

    const baseEncodingId = genIdPro.generateBaseEncoding('', '', 18, 36);
    console.log(baseEncodingId);
  6. Generate UUID-style IDs Generate a UUID-style ID:

    const uuidId = genIdPro.generateUUID();
    console.log(uuidId);
  7. Generate Expiration-based IDs Generate an ID with expiration (TTL):

    const ttlId = genIdPro.generateWithExpiration('', '', 60, 18);
    console.log(ttlId);
  8. Generate IDs with Custom Separator Generate an ID with a custom separator:

    const customSeparatorId = genIdPro.generateWithCustomSeparator('', '', '-', 18);
    console.log(customSeparatorId);
  9. Generate IDs with Dynamic Prefix/Suffix Generate an ID with dynamic prefix and suffix:

    const dynamicPrefixSuffixId = genIdPro.generateWithDynamicPrefixSuffix(18);
    console.log(dynamicPrefixSuffixId);
  10. Generate Project-Based IDs Generate a unique ID including a project name (max 7 characters):

    const projectId = genIdPro.generateProjectId('MyProjectName', 36);
    console.log(projectId);
  11. Decode an ID Decode a generated ID into its parts:

    const decodedId = genIdPro.decode(uniqueId);
    console.log(decodedId);
  12. Validate ID Length Validate the length of an ID:

    const isValidLength = genIdPro.validateIdLength(uniqueId, 36);
    console.log(isValidLength);
  13. Change the ID Structure Change an existing ID's structure:

    const changedId = genIdPro.change(uniqueId, 'newPrefix-', '-newSuffix', 20);
    console.log(changedId);

Methods Overview

Method Description
generate(prefix, suffix, length) Generates a random ID with custom prefix, suffix, and length.
generateUnique(prefix, suffix, length) Generates a unique ID, ensuring no duplicates.
generateTimeBasedId(prefix, suffix, length) Generates a time-based ID using the current timestamp.
generateBaseEncoding(prefix, suffix, length, base) Generates a Base-encoded ID in the specified base (36 or 64).
generateUUID() Generates a UUID-style ID using timestamp and random entropy.
generateWithExpiration(prefix, suffix, ttlMinutes, length) Generates an ID with expiration time (TTL in minutes).
generateWithCustomSeparator(prefix, suffix, separator, length) Generates an ID with custom separator between parts.
generateWithDynamicPrefixSuffix(length) Generates an ID with dynamic prefix and suffix based on timestamp and random part.
generateProjectId(projectName, length) Generates a unique ID including a customizable project name (max 7 characters).
decode(id) Decodes a generated ID into its components.
validateIdLength(id, maxLength) Validates that the ID length does not exceed a specified maximum length.
change(id, newPrefix, newSuffix, newLength) Changes an existing ID's prefix, suffix, or length.
checkUniqueness(id) Checks if an ID is unique (not already generated).

License

This package is licensed under the MIT License.

Package Sidebar

Install

npm i genid-pro

Weekly Downloads

544

Version

1.0.15

License

MIT

Unpacked Size

31.8 kB

Total Files

8

Last publish

Collaborators

  • dev_abhay