Created
March 9, 2018 04:16
-
-
Save ryochin/8cac01db2730ceaf47fe8879ff695ac2 to your computer and use it in GitHub Desktop.
simple but practical js password generator
This file contains hidden or 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
# npm install lodash | |
# coffee -bc generate_password.coffee && node ./generate_password.js | |
`const _ = require('lodash')` | |
charList = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789" # omit o,O,0 | |
symbolList = "._-=[]{}+#^!?" | |
symbolListEscaped = _.reduce symbolList.split(''), ((r, s) -> r += "\\#{s}"), '' | |
generatePassword = (len) -> | |
pickOne = (n) -> | |
# normal chars on first and last ones | |
list = if n == 0 or n == len - 1 then charList else charList + symbolList | |
list[Math.floor(Math.random() * list.length)] | |
countSymbol = (str) -> | |
(str.match(new RegExp(///[#{symbolListEscaped}]///, 'g')) || []).length | |
check = (str) -> | |
# omit successive chars | |
return false if str.match /(.)\1\1/ | |
# confirm the str contains 1..2 symbols | |
return false if countSymbol(str) == 0 | |
return false if countSymbol(str) > 2 | |
true | |
while 1 | |
result = _.reduce [0..len - 1], ((r, n) -> r += pickOne(n)), '' | |
return result if check result | |
for [0..20] | |
console.log generatePassword 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment