|
| 1 | +/* eslint-disable no-loop-func */ |
| 2 | +const resultEl = document.getElementById('result'); |
| 3 | +const lengthEl = document.getElementById('length'); |
| 4 | +const uppercaseEl = document.getElementById('uppercase'); |
| 5 | +const lowercaseEl = document.getElementById('lowercase'); |
| 6 | +const numbersEl = document.getElementById('numbers'); |
| 7 | +const symbolsEl = document.getElementById('symbols'); |
| 8 | +const generateEl = document.getElementById('generate'); |
| 9 | +const clipboardEl = document.getElementById('clipboard'); |
| 10 | + |
| 11 | +clipboardEl.addEventListener('click', () => { |
| 12 | + const textarea = document.createElement('textarea'); |
| 13 | + const password = resultEl.innerText; |
| 14 | + |
| 15 | + if (!password) { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + textarea.value = password; |
| 20 | + document.body.appendChild(textarea); |
| 21 | + textarea.select(); |
| 22 | + document.execCommand('copy'); |
| 23 | + textarea.remove(); |
| 24 | + alert('Password copied to clipboard!'); |
| 25 | +}); |
| 26 | + |
| 27 | +const getRandomLower = () => String.fromCharCode(Math.floor(Math.random() * 26) + 97); |
| 28 | + |
| 29 | +const getRandomUpper = () => String.fromCharCode(Math.floor(Math.random() * 26) + 65); |
| 30 | + |
| 31 | +const getRandomNumber = () => String.fromCharCode(Math.floor(Math.random() * 10) + 48); |
| 32 | + |
| 33 | +const getRandomSymbol = () => { |
| 34 | + const symbols = '!@#$%^&*(){}[]=<>/,.'; |
| 35 | + return symbols[Math.floor(Math.random() * symbols.length)]; |
| 36 | +}; |
| 37 | + |
| 38 | +const randomFunc = { |
| 39 | + lower: getRandomLower, |
| 40 | + upper: getRandomUpper, |
| 41 | + number: getRandomNumber, |
| 42 | + symbol: getRandomSymbol, |
| 43 | +}; |
| 44 | +const generatePassword = (lower, upper, number, symbol, length) => { |
| 45 | + let generatedPassword = ''; |
| 46 | + const typesCount = lower + upper + number + symbol; |
| 47 | + const typesArr = [{ lower }, { upper }, { number }, { symbol }].filter( |
| 48 | + (item) => Object.values(item)[0], |
| 49 | + ); |
| 50 | + |
| 51 | + if (typesCount === 0) { |
| 52 | + return ''; |
| 53 | + } |
| 54 | + |
| 55 | + for (let i = 0; i < length; i += typesCount) { |
| 56 | + typesArr.forEach((type) => { |
| 57 | + const funcName = Object.keys(type)[0]; |
| 58 | + generatedPassword += randomFunc[funcName](); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + const finalPassword = generatedPassword.slice(0, length); |
| 63 | + |
| 64 | + return finalPassword; |
| 65 | +}; |
| 66 | +generateEl.addEventListener('click', () => { |
| 67 | + const length = +lengthEl.value; |
| 68 | + const hasLower = lowercaseEl.checked; |
| 69 | + const hasUpper = uppercaseEl.checked; |
| 70 | + const hasNumber = numbersEl.checked; |
| 71 | + const hasSymbol = symbolsEl.checked; |
| 72 | + |
| 73 | + resultEl.innerText = generatePassword( |
| 74 | + hasLower, |
| 75 | + hasUpper, |
| 76 | + hasNumber, |
| 77 | + hasSymbol, |
| 78 | + length, |
| 79 | + ); |
| 80 | +}); |
0 commit comments