import COMMON_PASSWORDS from "./common-passwords" ; import { SequenceMatcher } from "./difflib" ; export function minLength ( value , minLength ) { return value . length < minLength ; } export function common ( value ) { return COMMON_PASSWORDS . indexOf ( value ) !== - 1 ; } export function hasNonNumeric ( value ) { return value . length === 0 || /^\d+$/ . test ( value ) ; } export function similar ( value , disallowed ) { if ( ! value || ! disallowed ) { return false ; } else { const isAllowed = disallowed . every ( disallowedVal => { if ( ! disallowedVal ) { return true ; } else { const parts = disallowedVal . split ( /\W+/ ) . concat ( disallowedVal ) ; return parts . every ( part => { const matcher = new SequenceMatcher ( false , part . toLowerCase ( ) , value . toLowerCase ( ) ) ; return matcher . quickRatio ( ) < 0.7 ; } ) ; } } ) ; return ! isAllowed ; } } export function validate ( value , userAttributes ) { return { minLength : minLength ( value , 8 ) , common : common ( value ) , hasNonNumeric : hasNonNumeric ( value ) , similar : similar ( value , userAttributes ) } ; }