diff --git a/src/functions/generatePassword.ts b/src/functions/generatePassword.ts new file mode 100644 index 0000000..787b2fe --- /dev/null +++ b/src/functions/generatePassword.ts @@ -0,0 +1,35 @@ +/** + * Generate a random password + * @param length The length of the password + * @param options The options for the password + */ +export function generatePassword( + length: number, + options: GeneratePasswordOptions +) { + const upper = options.upper || false; + const lower = options.lower || false; + const numbers = options.numbers || false; + const special = options.special || false; + const upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const lowerChars = "abcdefghijklmnopqrstuvwxyz"; + const numberChars = "0123456789"; + const specialChars = "!@#$%^&*()_+-=[]{}|;':\",./<>?"; + let password = ""; + let chars = ""; + if (upper) chars += upperChars; + if (lower) chars += lowerChars; + if (numbers) chars += numberChars; + if (special) chars += specialChars; + for (let i = 0; i < length; i++) { + password += chars.charAt(Math.floor(Math.random() * chars.length)); + } + return password; +} + +export interface GeneratePasswordOptions { + upper: boolean; + lower: boolean; + numbers: boolean; + special: boolean; +} diff --git a/src/index.d.ts b/src/index.d.ts index 8afe69c..846b2c3 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -40,3 +40,7 @@ export { superscript } from "./functions/superscript"; export { doublestruck } from "./functions/doublestruck"; export { fractur } from "./functions/fractur"; export { round } from "./functions/round"; +export { + generatePassword, + GeneratePasswordOptions, +} from "./functions/generatePassword"; diff --git a/src/index.ts b/src/index.ts index fe89d65..5303173 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,6 +45,10 @@ export { doublestruck } from "./functions/doublestruck"; export { fractur } from "./functions/fractur"; export { randomID } from "./functions/randomID"; export { round } from "./functions/round"; +export { + generatePassword, + GeneratePasswordOptions, +} from "./functions/generatePassword"; const config: ConfigURLS = { api: "https://api.night0721.me",