All files index.ts

100% Statements 38/38
100% Branches 32/32
100% Functions 3/3
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x 1x 1x 1x                                       54x 54x 54x 1x   53x 200x 200x 200x 200x 200x 142x   200x 153x   200x 115x   200x 109x     53x         4x 4x 27x   4x                           17x 17x 17x 17x 17x   17x 17x 68x 35x     17x    
export const LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';
export const UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
export const NUMERIC = '0123456789';
export const SPECIAL_CHARACTER = '!@#$%^&*()_+~`|}{\\[\\]:;?>,.<-=\\/';
 
export type Option = {
  /**
   * Integer, length of password.
   * @default 10
   */
  length?: number;
  /** Boolean, put lowercase in password */
  lowerCase?: boolean;
  /** Boolean, use uppercase letters in password. */
  upperCase?: boolean;
  /** Boolean, put numbers in password. */
  numeric?: boolean;
  /** Special characters */
  special?: boolean;
};
 
/** Create a random password */
export function generate(opts: Option = {}) {
  const { lowerCase = true, upperCase = true, numeric = true, special = true, length = 10 } = opts;
  let password = '';
  if (!lowerCase && !upperCase && !numeric && !special) {
    return password;
  }
  while (password.length < length) {
    const entity1 = Math.ceil(LOWERCASE.length * Math.random() * Math.random()) - 1;
    const entity2 = Math.ceil(NUMERIC.length * Math.random() * Math.random()) - 1;
    const entity3 = Math.ceil(SPECIAL_CHARACTER.length * Math.random() * Math.random()) - 1;
    const entity4 = Math.ceil(UPPERCASE.length * Math.random() * Math.random()) - 1;
    if (lowerCase && password.length < length) {
      password += LOWERCASE.charAt(entity1);
    }
    if (upperCase && password.length < length) {
      password += UPPERCASE.charAt(entity4);
    }
    if (numeric && password.length < length) {
      password += NUMERIC.charAt(entity2);
    }
    if (special && password.length < length) {
      password += SPECIAL_CHARACTER.charAt(entity3);
    }
  }
  return password.trim();
}
 
/** Create a random set of passwords */
export function generateMultiple(length: number = 10, opts?: Option) {
  const result: string[] = [];
  for (let i = 0; i < length; i++) {
    result.push(generate(opts));
  }
  return result;
}
 
/**
 * symbols pass with lowercase and uppercase letters, numbers and special characters
 * @return [0~4]
 *
 * `4` Strong :) Now it's safe!
 * `3` Medium level. Enter more symbols!
 * `2` Very Weak! It's easy to crack!
 * `1` It's easy to crack!
 */
export function validate(password: string = '') {
  // Create an array and push all possible values that you want in password
  const matchedCase = new Array();
  matchedCase.push(`[${SPECIAL_CHARACTER}]`); // Special Charector
  matchedCase.push('[A-Z]'); // Uppercase Alpabates
  matchedCase.push('[0-9]'); // Numbers
  matchedCase.push('[a-z]'); // Lowercase Alphabates
  // Check the conditions
  let ctr = 0;
  for (let i = 0; i < matchedCase.length; i++) {
    if (new RegExp(matchedCase[i]).test(password)) {
      ctr++;
    }
  }
  return ctr;
}