randcraft
v1.0.1
Published
A utility library for generating random numbers, strings, and validating phone numbers across various formats.
Downloads
6
Maintainers
Readme
Randcraft
A utility library for generating random numbers, strings, and validating phone numbers. Randcraft provides developers with essential functions to handle randomness and phone number validation making it easier to integrate these features into applications.
Features
- Generate Random Numbers: Easily create random integers and floating-point numbers within specified ranges.
- Generate Random Strings: Create random strings with customizable lengths and character sets.
- Phone Number Validation: Validate phone numbers based on various international formats, ensuring they conform to the correct structure for different countries.
Phone Number Validation
The phone number validation function allows you to verify if a given phone number is valid according to specific country formats. Supported countries include:
- United States: Formats like (123) 456-7890 or 123-456-7890
- United Kingdom: Formats like +447123456789 or 07123 456789
- Georgia: Formats like +995 5XX XXX XXX or +9955XX-XXX-XXX
- And more: The library supports validation for multiple popular countries.
Usage
To use the library, first install it via npm:
npm install randcraft
Importing Functions
Using CommonJS (Node.js) const { generateRandomNumber, generateRandomString, validatePhoneNumber } = require('randcraft');
Using ES6 Modules If you're using a module bundler or an environment that supports ES6 modules, you can import the library as follows: import { generateRandomNumber, generateRandomString, validatePhoneNumber } from 'randcraft';
Public Methods validatePhoneNumber(phoneNumber, country = 'GE') Validates a phone number based on common patterns for different countries. The country parameter can be specified to validate against a specific country's format (default is Georgia). The function also allows validation without the country code.
Parameters:
phoneNumber (string): The phone number to validate. country (string, optional): The country code (default is 'GE' for Georgia). Returns: true if the phone number is valid; otherwise, false. console.log(validatePhoneNumber("+995 599 123 456")); // true (valid Georgian number) console.log(validatePhoneNumber("599 123 456")); // true (valid Georgian number without country code) console.log(validatePhoneNumber("+1 123 456 7890", 'US')); // true (valid US number)
Parameters:
min (number): The minimum value (inclusive). max (number): The maximum value (inclusive). Returns: A random integer between min and max.
Example: const randomNumber = generateRandomNumber(1, 100); console.log(randomNumber); // Example output: 42
generateRandomString(length) Generates a random string of the specified length using alphanumeric characters.
Parameters:
length (number): The length of the generated string. Returns: A random alphanumeric string.
Example: const randomString = generateRandomString(10); console.log(randomString); // Example output: 'a1b2c3d4e5' License This project is licensed under the MIT License. Author Arsen Akhalaia