UNPKG

otpgeneratorpro

Version:

An advanced OTP generator with customizable features.

46 lines (38 loc) 1.54 kB
// index.js /** * Generates an OTP with customizable options. * @param {number} [length=10] - The length of the OTP. * @param {Object} [options] - Optional settings for OTP generation. * @param {boolean} [options.digits=true] - Include digits in the OTP. * @param {boolean} [options.lowerCaseAlphabets=true] - Include lowercase alphabets. * @param {boolean} [options.upperCaseAlphabets=true] - Include uppercase alphabets. * @param {boolean} [options.specialChars=true] - Include special characters. * @returns {string} - The generated OTP. */ function generateOtp(length = 10, options = {}) { const { digits = true, lowerCaseAlphabets = true, upperCaseAlphabets = true, specialChars = true } = options; const digitChars = '0123456789'; const lowerChars = 'abcdefghijklmnopqrstuvwxyz'; const upperChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; const specialCharsSet = '!@#$%^&*()-_=+[]{}|;:,.<>?'; let chars = ''; if (digits) chars += digitChars; if (lowerCaseAlphabets) chars += lowerChars; if (upperCaseAlphabets) chars += upperChars; if (specialChars) chars += specialCharsSet; if (chars.length === 0) { throw new Error('At least one character type should be included.'); } let otp = ''; for (let i = 0; i < length; i++) { const randomIndex = Math.floor(Math.random() * chars.length); otp += chars[randomIndex]; } return otp; } module.exports = { generateOtp };