rluiten / mailcheck / Mailcheck

A library that suggests a correct domain when a user miss spells an email address. This is a port of this javascript library https://github.com/mailcheck/mailcheck

Basic Usage

    Mailcheck.suggest 'test

Create

suggest : String -> Maybe ( String, String, String )

Suggest a domain which may assist a user with a possible error in a candidate email address. This version uses the default internal lists of domains.

    suggest 'test@gmail.co'

is equivalent to

    suggestWith defaultDomains defaultSecondLevelDomains defaultTopLevelDomains 'test@gmail.co'

example

    (suggest "user@gmil.com")
      == Just ("user", "gmail.com", "user@gmail.com")

Result is Maybe (address, domain, secondLevelDomain, topLevelDomain)

suggestWith : List String -> List String -> List String -> String -> Maybe ( String, String, String )

Suggest with passed in domain lists.

Utility

encodeEmail : String -> Maybe String

Encode the email address to prevent XSS but leave in valid characters, following this official spec: http://en.wikipedia.org/wiki/Email_address#Syntax

This is exported to test it.

encodeURI() will not encode: ~!@#$&*()=:/,;?+' Elm's Url.percentEncode actually calls encodeURIComponent

encodeURIComponent() escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )

Extra rules were added since Elm provides encodeURIComponent() functionality.

    (encodeEmail "<hello>@domain.com") == "%3Chello%3E@domain.com"

mailParts : String -> Maybe MailParts

Split an email address up into components.

    (mailParts "user") == Nothing
    (mailParts "user@") == Nothing
    (mailParts "user@moo.com") ==
      Just
      ( { topLevelDomain = "user"
        , secondLevelDomain = "moo.com"
        , domain = "moo"
        , address = "com"
        }
      )
    (mailParts "user@moo.co.uk") ==
      Just
      ( { topLevelDomain = "user"
        , secondLevelDomain = "moo.com.uk"
        , domain = "moo"
        , address = "co.uk"
        }
      )


type alias MailParts =
{ topLevelDomain : String
, secondLevelDomain : String
, domain : String
, address : String 
}

Record type alias for mailparts.

findClosestDomain : String -> List String -> Maybe String

Find closest domain in given list of domains and threshold using default distance.

    findClosestDomain "test@gmail.co" slds tlds

is equivalent to

    findClosestDomainWith sift3Distance topLevelThreshold "test@gmail.co" slds tlds

Default domain lists used by suggest

defaultDomains : List String

default list of domains used in suggest

defaultTopLevelDomains : List String

default list of top level domains used in suggest

defaultSecondLevelDomains : List String

default list of second level domains used in suggest