UNPKG

otp-generator-module

Version:

A simple and flexible OTP (One-Time Password) generator module for JavaScript and TypeScript. Supports numeric, alphabetic, and special character combinations with customizable options.

20 lines (19 loc) 675 B
export const generateOTP = (options = {}) => { const { upperCase = false, alphabets = false, specialChars = false, length = 4, } = options; let characters = '0123456789'; if (alphabets) characters += 'abcdefghijklmnopqrstuvwxyz'; if (upperCase) characters += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; if (specialChars) characters += '!@#$%^&*()'; if (!characters.length) { throw new Error('No character sets selected for OTP generation.'); } let otp = ''; for (let i = 0; i < length; i++) { const index = Math.floor(Math.random() * characters.length); otp += characters[index]; } return otp; };