Created
September 13, 2019 19:56
-
-
Save Kirill255/2d08c1cdfa59df8b259b3a4e164d944f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const random = (length, chars) => { | |
let mask = ""; | |
if (chars.indexOf("a") > -1) mask += "abcdefghijklmnopqrstuvwxyz"; | |
if (chars.indexOf("A") > -1) mask += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
if (chars.indexOf("#") > -1) mask += "0123456789"; | |
if (chars.indexOf("!") > -1) mask += "~`!@#$%^&*()_+-={}[]:\";'<>?,./|\\"; | |
let result = ""; | |
for (let i = length; i > 0; --i) { | |
result += mask[Math.round(Math.random() * (mask.length - 1))]; | |
} | |
return result; | |
}; | |
random(20, "aA#!"); | |
// TdIXwXCO\Q#Rse68H:"O | |
console.log(random(20, "aA#")); | |
// 20ZomWHbTkPvMjXfSxE9 | |
random(20, "A#"); | |
// 271GLIZB4DD4NU3KVZM3 | |
random(20, "aA"); | |
// BoIkVWcMVcCDCohUBcLc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment