github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/presentation/mailboxvalidator/score.go (about)

     1  package mailboxvalidator
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  )
     7  
     8  var hasNumberInUserNameRE = regexp.MustCompile(`.*\d.*?`)
     9  var hasDotInUsername = regexp.MustCompile(`.*\..*?`)
    10  var minScore = 1
    11  
    12  // CalculateScore calculates scores for the MailboxvalidatorScore field
    13  func CalculateScore(presentation DepPresentation) float64 {
    14  	score := minScore
    15  
    16  	if presentation.IsDomain.ToBool() && presentation.IsSyntax.ToBool() {
    17  		score += 9
    18  		if presentation.IsSMTP.ToBool() {
    19  			score += 10
    20  		}
    21  		if presentation.IsVerified.ToBool() {
    22  			score += 40
    23  
    24  			if presentation.IsDisposable.ToBool() {
    25  				score = 30
    26  				if presentation.IsCatchall.ToBool() {
    27  					score -= 5
    28  				}
    29  			} else {
    30  				if !presentation.IsFree.ToBool() {
    31  					score += 39
    32  					if presentation.IsCatchall.ToBool() {
    33  						score -= 44
    34  					} else if presentation.IsRole.ToBool() {
    35  						score -= 39
    36  					}
    37  				} else if presentation.IsCatchall.ToBool() {
    38  					score -= 5
    39  				}
    40  			}
    41  		}
    42  	}
    43  	if score < minScore {
    44  		score = minScore
    45  	}
    46  
    47  	emailAddressLength := len(presentation.EmailAddress)
    48  	if emailAddressLength == 0 {
    49  		return 0
    50  	}
    51  	pos := strings.IndexByte(presentation.EmailAddress, '@')
    52  	if pos == -1 {
    53  		pos = emailAddressLength - 1
    54  	}
    55  
    56  	username := presentation.EmailAddress[:pos]
    57  
    58  	if hasNumberInUserNameRE.MatchString(username) {
    59  		score -= 2
    60  	}
    61  	if hasDotInUsername.MatchString(username) {
    62  		score++
    63  	}
    64  
    65  	return float64(score) / 100.0
    66  }