import zxcvbn , { ZXCVBNResult , ZXCVBNScore } from 'zxcvbn' ; const truncateCpuDemandingPassword = ( input ) => input . substr ( 0 , 100 ) ; const requiredStrengthScore = 4 ; const requiredPasswordLength = 12 ; function hasHighestPasswordScore ( score ) { return score >= requiredStrengthScore ; } function hasSufficientLength ( input ) { return input . length >= requiredPasswordLength ; } interface ValidatedPassword extends ZXCVBNResult { meetsLengthRequirement : boolean ; meetsScoreRequirement : boolean ; meetsAllStrengthRequirements : boolean ; } export function validatePassword ( input ) { const password = input . length > 100 ? truncateCpuDemandingPassword ( input ) : input ; const result = zxcvbn ( password ) ; const meetsScoreRequirement = hasHighestPasswordScore ( result . score ) ; const meetsLengthRequirement = hasSufficientLength ( input ) ; const meetsAllStrengthRequirements = meetsScoreRequirement && meetsLengthRequirement ; return Object . freeze ( { ... result , meetsScoreRequirement , meetsLengthRequirement , meetsAllStrengthRequirements , } ) ; } export const blankPasswordValidation = Object . freeze ( validatePassword ( '' ) ) ;