Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
ui-component-library / src / forms / deps / isValidPassword.ts
Size: Mime:
import { size } from 'chain-able-boost'
import { isString, isSafe } from 'exotic'
import isValidLength from './isValidLength'
import { matchLetters, matchNumbers } from './isAlphaNumeric'
import { test } from './__match'

// const matchPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!*])(?=.{8,})/

/**
 * @param {string} [value='']
 * @return {boolean}
 */
function isValidPassword(value: string = ''): boolean {
  // Commenting this out as the validation has to happen on the server-side
  // return isString(value) && matchPassword.test(value)

  // Performing on the length validation
  return isValidLength(value, 5)
}

// === strength ===

/* eslint-disable max-statements */
const strengthList = {
  '0': 'very weak',
  '1': 'weak',
  // medium
  '2': 'good',
  '3': 'strong',
  '5': 'very strong',
}

/**
 * @param {Number} strength
 * @return {String}
 */
function toStrengthName(strength = 0) {
  // default to very weak / 0
  const wording = strengthList[0]

  // start at highest
  while (strength >= 0) {
    const wordForStrength = strengthList[strength]
    if (isSafe(wordForStrength)) {
      return wordForStrength
    }

    // go lower until we find the right word
    strength -= 1
  }

  return wording
}

// .*[!,%,&,@,#,$,^,*,?,_,~]
const matchSpecialChar = /(.*[!,%,&,@,#,$,^,*,?,_,~])/g

// if it has one special character, increase strength value
const hasAnySpecialChar = test(matchSpecialChar)

// if it has two special characters, increase strength value
const hasSpecialChar = (x, min = 1) =>
  hasAnySpecialChar(x) && x.match(min).length > min

// if password contains both lower and uppercase characters, increase strength value
const matchLowercaseAndUppercase = /([a-z].*[A-Z])|([A-Z].*[a-z])/
const hasLowercaseAndUppercase = test(matchLowercaseAndUppercase)

// if it has numbers and characters, increase strength value
const hasNumbersAndLetters = x => test(matchNumbers, x) && test(matchNumbers, x)

// if length is 8 characters or more, increase strength value
// @note @VARIATION (BY 2 - THIS IS THE BIGGEST FACTOR)
const isLongEnough = validPassword => validPassword.length > 8

/**
 * @todo - move to best module later
 * @see toStrengthName
 *
 * @param {String} validPassword
 * @return {String} configurable wording for strength at a certain Weight
 */
function toStrength(validPassword) {
  let strength = 0

  if (isLongEnough(validPassword)) {
    strength += 2
  }
  if (hasLowercaseAndUppercase(validPassword)) {
    strength += 1
  }
  if (hasNumbersAndLetters(validPassword)) {
    strength += 1
  }
  if (hasSpecialChar(validPassword)) {
    strength += 1
  }
  if (hasSpecialChar(validPassword, 2)) {
    strength += 1
  }

  // now we have calculated strength value, we can return messages

  return toStrengthName(strength)
}

export { toStrength }
export { toStrengthName }
export { isValidPassword }
export default isValidPassword