-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_password.js
More file actions
52 lines (45 loc) · 1.52 KB
/
Copy pathgenerate_password.js
File metadata and controls
52 lines (45 loc) · 1.52 KB
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
// generate_password.js
const sample = (array) => {
const index = Math.floor(Math.random() * array.length)
return array[index]
}
// define generatePassword function
const generatePassword = (options) => {
// define things user might want
const lowerCaseLetters = "abcdefghijklmnopqrstuvwxyz"
const upperCaseLetters = lowerCaseLetters.toUpperCase()
const numbers = "1234567890"
const symbols = '`~!@$%^&*()-_+={}[]|;:"<>,.?/'
// create a collection to store things user picked up
let collection = []
if (options.lowercase === "on") {
collection = collection.concat(lowerCaseLetters.split(""))
}
if (options.uppercase === "on") {
collection = collection.concat(upperCaseLetters.split(""))
}
if (options.numbers === "on") {
collection = collection.concat(numbers.split(""))
}
if (options.symbols === "on") {
collection = collection.concat(symbols.split(""))
}
// remove things user do not need
if (options.excludeCharacters) {
collection = collection.filter(
character => !options.excludeCharacters.includes(character)
)
}
if (collection.length === 0) {
return 'There is no valid character in your selection.'
}
// start generating password
let password = ""
for (let i = 0; i < Number(options.length); i++) {
password += sample(collection)
}
// return the generated password
return password
}
// invoke generatePassword function
module.exports = generatePassword