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.
- 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.
- You can specify the length of the generated ID.
- The format can be customized with prefixes, suffixes, and separators.
- The
generateTimeBasedId()
method generates IDs based on the current timestamp, ensuring a time-ordered sequence of IDs.
- 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.
- The
generateUUID()
method generates IDs similar to UUID format, using the current timestamp and random entropy.
- 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.
- The
generateWithCustomSeparator()
method allows you to customize the separator used between different parts of the ID (e.g., timestamp, process ID, etc.).
- 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.
- 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.
- The
validateIdLength()
method ensures the generated ID does not exceed a specified length.
- 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.
- The library generates unique machine and process identifiers using the system's MAC address (or hostname as fallback) and the process ID.
- 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.
To install genIdPro
in your project, run the following command:
npm install genid-pro
-
Import the Library
const genIdPro = require('genid-pro');
-
Initialize the Library Call
init()
to initialize the machine and process IDs:genIdPro.init();
-
Generate Unique IDs Generate a unique ID with a customizable prefix, suffix, and length:
const uniqueId = genIdPro.generateUnique('', '', 18); console.log(uniqueId);
-
Generate Time-Based IDs Generate an ID based on the current timestamp:
const timestampId = genIdPro.generateTimeBasedId('', '', 18); console.log(timestampId);
-
Generate Base Encoding IDs Generate an ID in Base-36 encoding:
const baseEncodingId = genIdPro.generateBaseEncoding('', '', 18, 36); console.log(baseEncodingId);
-
Generate UUID-style IDs Generate a UUID-style ID:
const uuidId = genIdPro.generateUUID(); console.log(uuidId);
-
Generate Expiration-based IDs Generate an ID with expiration (TTL):
const ttlId = genIdPro.generateWithExpiration('', '', 60, 18); console.log(ttlId);
-
Generate IDs with Custom Separator Generate an ID with a custom separator:
const customSeparatorId = genIdPro.generateWithCustomSeparator('', '', '-', 18); console.log(customSeparatorId);
-
Generate IDs with Dynamic Prefix/Suffix Generate an ID with dynamic prefix and suffix:
const dynamicPrefixSuffixId = genIdPro.generateWithDynamicPrefixSuffix(18); console.log(dynamicPrefixSuffixId);
-
Generate Project-Based IDs Generate a unique ID including a project name (max 7 characters):
const projectId = genIdPro.generateProjectId('MyProjectName', 36); console.log(projectId);
-
Decode an ID Decode a generated ID into its parts:
const decodedId = genIdPro.decode(uniqueId); console.log(decodedId);
-
Validate ID Length Validate the length of an ID:
const isValidLength = genIdPro.validateIdLength(uniqueId, 36); console.log(isValidLength);
-
Change the ID Structure Change an existing ID's structure:
const changedId = genIdPro.change(uniqueId, 'newPrefix-', '-newSuffix', 20); console.log(changedId);
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). |
This package is licensed under the MIT License.