password-generation
Version:
Password generation and complexity checking without headache
59 lines (48 loc) • 1.29 kB
text/typescript
export const specials = "?:;\'$*\=+,!.`~\"%^/#@|&";
export const charsLowerCase = "abcdefghijklmnopqrstuvwxyz";
export const charsUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
export const charsNumber = "1234567890";
/**
* Shuffle array values using Fisher-Yates algorithm
*
* @param {Array} array Source array
*
* @returns {Array} Shuffled array
*/
export const shuffle = (array: any[]) => {
let i = array.length;
while (i--) {
let j = Math.floor(Math.random() * (i + 1));
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
export const upperChar = () => {
return charFrom(charsUpperCase);
}
export const lowerChar = () => {
return charFrom(charsLowerCase);
}
export const anyChar = (key: string) => {
if (key === 'lowers') {
return charFrom(charsLowerCase);
}
if (key === 'uppers') {
return charFrom(charsUpperCase);
}
if (key === 'specials') {
return charFrom(specials);
}
return charFrom(charsNumber);
}
export const special = () => {
return charFrom(specials);
}
export const charFrom = (chars: string): string => {
return chars.charAt(random(chars.length - 1));
}
export const random = (max: number, min = 0): number => {
return Math.floor(Math.random() * (max - min + 1) + min);
}