UNPKG

@takentrade/takentrade-libs

Version:
28 lines (27 loc) 1.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateRandomString = generateRandomString; const DIGITS = '0123456789'; const LOWERCASE_CHARS = 'abcdefghijklmnopqrstuvwxyz'; const UPPERCASE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; const SPECIAL_CHARS = '!@#$%^&*()_+-=[]{}|;:,.<>?'; function generateRandomString(options = {}) { const { length = 6, digits = true, lowerCaseAlphabets = false, upperCaseAlphabets = false, specialChars = false, } = options; let chars = ''; if (digits) chars += DIGITS; if (lowerCaseAlphabets) chars += LOWERCASE_CHARS; if (upperCaseAlphabets) chars += UPPERCASE_CHARS; if (specialChars) chars += SPECIAL_CHARS; if (!chars) chars = DIGITS; // Default to digits if no character set is selected let result = ''; const charactersLength = chars.length; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * charactersLength)); } return result; }