All files / src/lib validate.js

100% Statements 13/13
100% Branches 14/14
100% Functions 5/5
100% Lines 13/13
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                36x 1x         29x 5x           77x         77x 77x 77x     77x           67x 5x         30x 1x                      
/*!
 * SuperGenPass library
 * https://github.com/chriszarate/supergenpass-lib
 * https://chriszarate.github.com/supergenpass/
 * License: GPLv2
 */
 
function validateCallback(callback) {
  if (typeof callback !== 'function') {
    throw new Error('Must provide callback function.');
  }
}
 
function validateLength(num) {
  if (num !== parseInt(num, 10) || num < 4 || num > 24) {
    throw new Error(`Length must be an integer between 4 and 24: ${num}.`);
  }
}
 
function validatePassword(str, length) {
  // Cut password to requested length.
  const password = str.substring(0, length);
 
  // 1. Password must start with a lowercase letter [a-z].
  // 2. Password must contain at least one uppercase letter [A-Z].
  // 3. Password must contain at least one numeral [0-9].
  const startsWithLowercaseLetter = /^[a-z]/;
  const containsUppercaseLetter = /[A-Z]/;
  const containsNumeral = /[0-9]/;
 
  // Return true if all tests are satisfied.
  return startsWithLowercaseLetter.test(password) &&
   containsUppercaseLetter.test(password) &&
   containsNumeral.test(password);
}
 
function validatePasswordInput(str) {
  if (typeof str !== 'string') {
    throw new Error(`Password must be a string, received ${typeof str}.`);
  }
}
 
function validatePasswordLength(str) {
  if (!str.length) {
    throw new Error('Combined password input must not be empty.');
  }
}
 
export {
  validateCallback,
  validateLength,
  validatePassword,
  validatePasswordInput,
  validatePasswordLength,
};