forked from Nixius/authelia
37 lines
746 B
Go
37 lines
746 B
Go
package accounts
|
|
|
|
import "strings"
|
|
|
|
func SlugFromEmail(email string) string {
|
|
parts := strings.SplitN(email, "@", 2)
|
|
local := parts[0]
|
|
domain := ""
|
|
if len(parts) == 2 {
|
|
domainParts := strings.Split(parts[1], ".")
|
|
if len(domainParts) >= 2 {
|
|
domain = "-" + domainParts[len(domainParts)-2]
|
|
}
|
|
}
|
|
return cleanSlug(local) + cleanSlug(domain)
|
|
}
|
|
|
|
func cleanSlug(s string) string {
|
|
out := strings.Map(func(r rune) rune {
|
|
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-' {
|
|
return r
|
|
}
|
|
if r == '_' {
|
|
return '-'
|
|
}
|
|
return '-'
|
|
}, strings.ToLower(s))
|
|
out = strings.Trim(out, "-")
|
|
if out == "" {
|
|
return "customer"
|
|
}
|
|
for strings.Contains(out, "--") {
|
|
out = strings.ReplaceAll(out, "--", "-")
|
|
}
|
|
return out
|
|
}
|