1.2.1 • Published 9 months ago

quickidgen v1.2.1

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

quickidgen

npm version

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

OptionTypeDefaultDescription
lengthNumber10Length of the generated ID
useNumbersBooleantrueWhether to include numbers in the ID
useLowercaseBooleantrueWhether to include lowercase letters
useUppercaseBooleantrueWhether to include uppercase letters
useSpecialCharsBooleantrueWhether to include special characters (e.g., @!#)
excludeCharsString""A string of characters to exclude from the ID generation
prefixString""Characters that appear at the beginning of the generated string
suffixString""Characters that appear at the end of the generated string
forWebBooleanfalseWhether to customize ID generation for web use cases
isUUIDBooleanfalseWhether to generate the ID in UUID format
customWordsArrArray[]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

Support

For support, you can contact me through my portfolio website or raise an issue on GitHub.

1.2.1

9 months ago

1.2.0

9 months ago

1.1.4

9 months ago

1.1.3

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.0

9 months ago