UNPKG

otp-number

Version:

> 🔐 A lightweight and flexible JavaScript library to generate random OTPs (One-Time Passwords) for use in both frontend and backend applications.

33 lines (26 loc) 878 B
function otp_number( length = 6, options = { digits: true, upperCase: false, lowerCase: false } ) { const sets = { digits: "0123456789", upperCase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", lowerCase: "abcdefghijklmnopqrstuvwxyz", }; let characters = ""; if (options.digits) characters += sets.digits; if (options.upperCase) characters += sets.upperCase; if (options.lowerCase) characters += sets.lowerCase; if (!characters) throw new Error("At least one character set must be enabled."); let otp = ""; for (let i = 0; i < length; i++) { otp += characters.charAt(Math.floor(Math.random() * characters.length)); } return otp; } // 👇 New helper for numeric-only OTPs function otp_number_only(length = 6) { return otp_number(length, { digits: true }); } module.exports = { otp_number, otp_number_only };