mpldr.codes/normalize-email Index | Examples | Files

package normalizeemail

import "mpldr.codes/normalize-email"

This package has the purpose of normalizing emails in order to prevent multiple signups with the same email address. Please note, that mail should always be sent to the user provided address in order to allow automatic sorting and not restricting the user unnecessarily.

By default adresses from hosts without special rules (see rules.go) have + subaddresses removed, and are transformed to lowercase. Special rules can be added per domain.

Index

Examples

Package Files

docs.go normalize.go rules.go

Variables

var (
    DefaultRuleset = Rule{
        SubAddressDelimiter: "+",
    }
    GmailRuleset = Rule{
        TrimChars: []rune{'.'},
    }
    GooglemailRuleset = Rule{
        TrimChars:    []rune{'.'},
        ChangeDomain: "gmail.com",
    }
    YahooRuleset = Rule{
        TrimChars:           []rune{'.'},
        SubAddressDelimiter: "-",
    }
    CockLiRuleset = Rule{
        SubAddressDelimiter: ".",
    }
)

func Normalize

func Normalize(email string) string

Normalize takes in the email and returns it in normalized form. This function does not perform validation and the normalized form of an invalid email address is an empty string.

Example

Code:

fmt.Println(Normalize("j.Oe+some-subaddress@googlemail.com"))

Output:

joe@gmail.com

func SetRule

func SetRule(domain string, rule Rule)

SetRule sets the rules applied to a domain. If the domain is an empty string, the rules overwrite the defaults.

Example

Code:

SetRule("some-provider.com", Rule{
    TrimChars:           []rune{'.'},
    SubAddressDelimiter: "-",
    ChangeDomain:        "new-provider-address.com",
})
fmt.Println(Normalize("eric.hoffman-subaddress@some-proviDER.Com"))

Output:

erichoffman@new-provider-address.com

type Rule

type Rule struct {
    // TrimChars is a list of characters that are removed from the
    // localpart of the email
    //
    // john.doe@gmail.com → johndoe@gmail.com
    TrimChars []rune
    // SubAddressDelimiter specifies what character ends the localpart and
    // marks the beginning of the subaddress
    //
    // joe+service@example.com → joe@example.com
    SubAddressDelimiter string
    // ChangeDomain specifies the domain these email-addresses are to be
    // changed to. To avoid infinite recursions, the rules of the new
    // domain are not applied.
    //
    // joe@googlemail.com -> joe@gmail.com
    ChangeDomain string
    // CaseSensitiveLocalpart indicates that a localpart must be in the
    // case specified. Otherwise, all characters are made lowercase. This
    // does not affect the domain, which is always turned to lowercase.
    //
    // JOE@DOE.com -> JOE@doe.com
    CaseSensitiveLocalpart bool
    // DisableDefaultRules disabled the default rules to be applied, in
    // case a provider has different rules.
    DisableDefaultRules bool
}
Version
v0.0.0-20220430103801-706557014365 (latest)
Published
Apr 30, 2022
Platform
linux/amd64
Imports
3 packages (graph)
Last checked
1 month ago

Tools for package owners.