Skip to content

Instantly share code, notes, and snippets.

@Zorgatone
Created August 25, 2017 08:52
Show Gist options
  • Save Zorgatone/6dba65ea50990cf21ea424775ee64e37 to your computer and use it in GitHub Desktop.
Save Zorgatone/6dba65ea50990cf21ea424775ee64e37 to your computer and use it in GitHub Desktop.
Generate password with custom character set and length
// EcmaScript 2015
export default class PasswordGenerator {
constructor(
charset = ""
) {
this.charset = charset.toString().split("").sort().reduce((a, b) => {
if (a.charAt(a.length - 1) === b) {
return a;
}
return a + b;
});
}
generate(length = 12) {
let password = "";
for (let i = 0, c = this.charset, l = this.charset.length; i < length; i += 1) {
password += this.charset.charAt(Math.floor(Math.random() * l));
}
return password;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment