github.com/wtfutil/wtf@v0.43.0/utils/email_addresses.go (about)

     1  package utils
     2  
     3  import (
     4  	"strings"
     5  
     6  	"golang.org/x/text/cases"
     7  	"golang.org/x/text/language"
     8  )
     9  
    10  // NameFromEmail takes an email address and returns the part that comes before the @ symbol
    11  //
    12  // Example:
    13  //
    14  //	NameFromEmail("test_user@example.com")
    15  //	> "Test_user"
    16  func NameFromEmail(email string) string {
    17  	parts := strings.Split(email, "@")
    18  	name := strings.ReplaceAll(parts[0], ".", " ")
    19  
    20  	c := cases.Title(language.English)
    21  	return c.String(name)
    22  }
    23  
    24  // NamesFromEmails takes a slice of email addresses and returns a slice of the parts that
    25  // come before the @ symbol
    26  //
    27  // Example:
    28  //
    29  //	NamesFromEmail("test_user@example.com", "other_user@example.com")
    30  //	> []string{"Test_user", "Other_user"}
    31  func NamesFromEmails(emails []string) []string {
    32  	names := make([]string, len(emails))
    33  
    34  	for i, email := range emails {
    35  		names[i] = NameFromEmail(email)
    36  	}
    37  
    38  	return names
    39  }