-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
126 lines (109 loc) · 3.52 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// HTML Imports
const password = document.getElementById('password')
const length = document.getElementById('length')
const range = document.getElementById('myRange')
const generateBtn = document.getElementById('btn')
const copyToClipboard = document.getElementById('copy-password')
const refreshBtn = document.getElementById('refresh-btn')
const upperCase = document.getElementById('toggle-btn-1')
const lowerCase = document.getElementById('toggle-btn-2')
const numbers = document.getElementById('toggle-btn-3')
const symbols = document.getElementById('toggle-btn-4')
// Utitity Variables/Objects
const settingObj = {
upper: getRandomUpper,
lower: getRandomLower,
numbers: getRandomNumber,
symbols: getRandomSymbol,
}
let passwordLength = 0
let hasUpperCase = true
let hasLowerCase = true
let hasNumbers = false
let hasSymbols = false
let isGenerated = false
// Event Listeners
range.addEventListener('input', (e) => {
length.textContent = e.target.value
})
generateBtn.addEventListener('click', () => {
passwordLength = +length.textContent
hasUpperCase = upperCase.checked
hasLowerCase = lowerCase.checked
hasNumbers = numbers.checked
hasSymbols = symbols.checked
const newPassword = generatePassword(
passwordLength,
hasUpperCase,
hasLowerCase,
hasNumbers,
hasSymbols
)
if (newPassword != '') {
password.textContent = newPassword
} else password.textContent = 'PASSWORD'
isGenerated = true
copyToClipboard.classList.add('allowed')
})
refreshBtn.addEventListener('click', () => {
passwordLength = +length.textContent
hasUpperCase = upperCase.checked
hasLowerCase = lowerCase.checked
hasNumbers = numbers.checked
hasSymbols = symbols.checked
const newPassword = generatePassword(
passwordLength,
hasUpperCase,
hasLowerCase,
hasNumbers,
hasSymbols
)
if (newPassword != '') {
password.textContent = newPassword
} else password.textContent = 'PASSWORD'
})
copyToClipboard.addEventListener('click', () => {
if (isGenerated || password.textContent != 'PASSWORD') {
navigator.clipboard.writeText(password.textContent)
document.body.classList.add('active-popup')
setTimeout(() => {
document.body.classList.remove('active-popup')
}, 1000)
}
})
// Utility Functions
function generatePassword(length, upper, lower, numbers, symbols) {
let newPassword = ''
const settingCount = upper + lower + numbers + symbols
if (settingCount === 0) {
return ''
}
const settingArr = [{ upper }, { lower }, { numbers }, { symbols }].filter(
(obj) => Object.values(obj)[0]
)
for (let i = 0; i < length; i += settingCount) {
const shuffledArray = settingArr.slice().sort((a, b) => Math.random() - 0.5)
shuffledArray.forEach((input) => {
const setting = Object.keys(input)[0]
newPassword += settingObj[setting]()
})
}
const finalPassword = newPassword.slice(0, length)
return finalPassword
}
function getRandomUpper() {
const upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
return upper[Math.floor(Math.random() * upper.length)]
}
function getRandomLower() {
const lower = 'abcdefghijklmnopqrstuvwxyz'
return lower[Math.floor(Math.random() * lower.length)]
}
function getRandomNumber() {
const numbers = '0123456789'
return numbers[Math.floor(Math.random() * numbers.length)]
}
function getRandomSymbol() {
const symbols = '!@#$%^&*(){}[]=<>/,.'
return symbols[Math.floor(Math.random() * symbols.length)]
}