quickidgen v1.2.1
quickidgen
quickidgen is a lightweight, customizable, and easy-to-use JavaScript utility for generating short and random unique IDs. It supports generating IDs with numbers, lowercase letters, uppercase letters, and special characters. You can also exclude specific characters based on your requirements, making it highly flexible for various use cases.
Key Features
- Customizable Length: Define the length of the ID (default is 10).
- Flexible Character Sets: Choose to include numbers, lowercase, uppercase letters, and special characters.
- Character Exclusion: Exclude specific characters from the generated ID.
- Prefix and Suffix: Add custom characters at the beginning and end of the generated ID.
- Lightweight: Minimal dependencies, easy to integrate.
- Randomness: Generates IDs that are random and unique for each use case.
- forWeb Functionality: Customize ID generation based on web use cases.
- UUID Generation: Generate IDs in UUID format with customizable parts.
- Error Boundary: Handles cases where no characters are available for ID generation.
- Readable ID Support: Ability to provide your own charset for generating IDs.
Installation
You can install quickidgen via npm:
npm install quickidgen
Usage
Using quickidgen
is simple and highly flexible. Here's an example of how to use it:
Basic Usage
const generateRandomId = require('quickidgen');
// Generate a random ID with default options
const id = generateRandomId();
console.log(id); // Example: 'a8G!5hT7@p'`
Custom Options
You can also customize the ID generation by passing an options object:
const generateRandomId = require('quickidgen');
// Generate a 15-character ID with only numbers and lowercase letters
const customId = generateRandomId({
length: 15,
useNumbers: true,
useLowercase: true,
useUppercase: false,
useSpecialChars: false,
});
console.log(customId); // Example: '8g7h9w3b1qz0t2y'`
Excluding Specific Characters
If you want to exclude specific characters, you can do so by specifying them in the excludeChars
option:
const generateRandomId = require('quickidgen');
// Generate an ID, excluding specific characters like '1' and 'a'
const idWithoutChars = generateRandomId({
excludeChars: '1a',
});
console.log(idWithoutChars); // Example: 'G!9T%bE@'`
Adding Prefix and Suffix
You can add custom characters at the beginning and end of the generated ID:
const generateRandomId = require('quickidgen');
// Generate an ID with a prefix and suffix
const idWithPrefixSuffix = generateRandomId({
prefix: 'ID-',
suffix: '-2024',
});
console.log(idWithPrefixSuffix); // Example: 'ID-a8G!5hT7@p-2024'
Web Functionality
Use the forWeb option to customize ID generation for web use cases:
const generateRandomId = require('quickidgen');
// Generate an ID optimized for web usage
const webId = generateRandomId({
forWeb: true,
});
console.log(webId); // Example: 'G!9T%bE@'
UUID Generation
To generate a UUID, use the isUUID option:
const generateRandomId = require('quickidgen');
// Generate a UUID
const uuid = generateRandomId({
isUUID: true,
});
console.log(uuid); // Example: '123e4567-e89b-12d3-a456-426614174000'
Readable ID
You can define your own character set for ID generation:
// Generate an ID with a custom character set
const customCharsetId = generateRandomId({
isReadable: true,
customWordsArr: ["Hello", "World"]
});
console.log(customCharsetId); // Example: 'HelloWorld'
Important Note
- If you want to generate a UUID
(isUUID: true)
, you cannot generate a readable ID(isReadable: true)
simultaneously. Please choose one option at a time.
Options
Option | Type | Default | Description |
---|---|---|---|
length | Number | 10 | Length of the generated ID |
useNumbers | Boolean | true | Whether to include numbers in the ID |
useLowercase | Boolean | true | Whether to include lowercase letters |
useUppercase | Boolean | true | Whether to include uppercase letters |
useSpecialChars | Boolean | true | Whether to include special characters (e.g., @!# ) |
excludeChars | String | "" | A string of characters to exclude from the ID generation |
prefix | String | "" | Characters that appear at the beginning of the generated string |
suffix | String | "" | Characters that appear at the end of the generated string |
forWeb | Boolean | false | Whether to customize ID generation for web use cases |
isUUID | Boolean | false | Whether to generate the ID in UUID format |
customWordsArr | Array | [] | An array of predefined words for generating readable IDs |
Changelog
1.2.0
- Added: Improved randomness algorithm for better uniqueness.
- Added: Support for generating readable IDs with predefined words using
customWordsArr
(even if you won't pass custom words predefined words will come). - Updated: Enhanced error handling for
excludeChars
option. - Fixed: Minor bug fixes and performance improvements.
Author
Vaibhav Kumar
- GitHub: prgVaibhav
- Portfolio: vaibhavkumar
Support
For support, you can contact me through my portfolio website or raise an issue on GitHub.