otp-number
v1.0.4
Published
> π A lightweight and flexible JavaScript library to generate random OTPs (One-Time Passwords) for use in both frontend and backend applications.
Maintainers
Readme
otp-number
π A lightweight and flexible JavaScript library to generate random OTPs (One-Time Passwords) for use in both frontend and backend applications.
β¨ Features
- β Supports digits, uppercase, and lowercase characters
- β Configurable OTP length
- β Works in Node.js and browser (UMD compatible)
- β Zero dependencies
- π’ Numeric-only OTP shortcut with
otp_number_only(length)
π¦ Installation
npm install otp-numberπ§ Usage
const { otp_number, otp_number_only } = require('otp-number');
// Generate a 6-digit OTP with digits, uppercase, and lowercase letters (default) const defaultOTP = otp_number(); console.log(defaultOTP); // Example: "aB3dE9"
// Generate an 8-digit numeric-only OTP using the helper function const numericOTP = otp_number_only(8); console.log(numericOTP); // Example: "12345678"
// Generate a 10-character OTP with only uppercase letters and digits const customOTP = otp_number(10, { digits: true, upperCase: true, lowerCase: false }); console.log(customOTP); // Example: "A1B2C3D4E5"
// Generate a 4-character OTP with only lowercase letters const lowercaseOTP = otp_number(4, { digits: false, upperCase: false, lowerCase: true }); console.log(lowercaseOTP); // Example: "wxyz"
// Attempting to generate an OTP with no character sets enabled will throw an error: try { otp_number(6, { digits: false, upperCase: false, lowerCase: false }); } catch (error) { console.error(error.message); // Output: "At least one character set must be enabled." }
Numeric Only Shortcut
const { otp_number_only } = require('otp-number');
console.log(otp_number_only(6)); // β€ "920384"
## Browser (UMD)
<script src="dist/otp-generator.umd.js"></script>
<script>
const otp = OtpGenerator.otp_number(6, { digits: true, upperCase: true });
console.log("OTP:", otp);
</script>
## βοΈ API
otp_number(length, options)
Parameter Type Default Description
length number 6 Length of the OTP
options.digits boolean true Include digits (0-9)
options.upperCase boolean false Include uppercase letters (A-Z)
options.lowerCase boolean false Include lowercase letters (a-z)
## π Example Outputs
otp_number();
// β€ "729314"
otp_number(6, { digits: true, upperCase: true });
// β€ "5J8K2Q"
otp_number(4, { lowerCase: true });
// β€ "kfza"
otp_number_only(length = 6)
A helper function that generates a numeric-only OTP string of the specified length. This is equivalent to calling otp_number(length, { digits: true }).
## π Build (for Browser Support)
Install dev dependencies:
bash
Copy
Edit
npm install --save-dev rollup @rollup/plugin-commonjs @rollup/plugin-node-resolve
Build UMD:
bash
Copy
Edit
npx rollup -c
## π Keywords
otp, otp-number, random otp, otp generator, javascript otp, nodejs otp, one-time password, verification code, auth code, frontend backend otp, browser otp, 2FA