UNPKG

otp-random-generator

Version:

generate the opt 4-digit and 6-digit

66 lines (47 loc) 1.92 kB
# OTP Generator A simple utility for generating random One-Time Passwords (OTPs) in JavaScript. Generates a random OTP of the specified length. It is designed to be simple and efficient, making it easy to integrate into your applications. ## Features - **Generates Numeric OTPs**: The generated OTPs consist of numeric digits only. - **Customizable Length**: You can specify the length of the OTP, with a default value of `6`. - **Simple and Easy to Use**: The function is straightforward to integrate into your applications. - **Handles Invalid Inputs Gracefully**: The function throws an error for invalid lengths, ensuring robust error handling. ## Installation To use this utility, you can simply copy the code into your project. If you are using Node.js, you can create a module as follows: ```bash npm install otp-random-generator ``` ## Usage Import the module in your Node.js project: ``` const { generateOTP } = require('otp-random-generator'); ``` #### Generate a 6-digit OTP (default length) ``` const otp6 = generateOTP(); console.log('6-digit OTP:', otp6); ``` #### Generate an 8-digit OTP ``` const otp8 = generateOTP(8); console.log('8-digit OTP:', otp8); ``` #### Generate a 4-digit OTP ``` const otp4 = generateOTP(4); console.log('4-digit OTP:', otp4); ``` ## Example Output ``` 6-digit OTP: 394857 8-digit OTP: 48392057 4-digit OTP: 1204 ``` ## Function: `generateOTP` ### Description The `generateOTP` function generates a random OTP of the specified length. It is designed to be simple and efficient, making it easy to integrate into your applications. ### Parameters - `length` (number): The length of the OTP to generate. The default value is `6`. The length must be a positive integer. ### Returns - A string representing the generated OTP. ### Throws - An error if the `length` is less than or equal to `0`.