Skip to content

Commit

Permalink
internet: cache Internet.User() first/last names (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
sean- authored Jun 12, 2023
1 parent 614e97a commit e792f3e
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ import (
"regexp"
"strconv"
"strings"
"sync"
)

var (
freeEmailDomain = []string{"gmail.com", "yahoo.com", "hotmail.com"}

tld = []string{"com", "com", "com", "com", "com", "com", "biz", "info", "net", "org"}

userFormats = []string{
"{{lastName}}.{{firstName}}",
"{{firstName}}.{{lastName}}",
"{{firstName}}",
"{{lastName}}",
}

emailFormats = []string{"{{user}}@{{domain}}", "{{user}}@{{freeEmailDomain}}"}

urlFormats = []string{
Expand All @@ -45,25 +39,51 @@ type Internet struct {

var validEmailOnlyValidCharacters = regexp.MustCompile(`[^a-z0-9._%+\-]+`)

func (Internet) transformIntoValidEmailName(name string) string {
func transformIntoValidEmailName(name string) string {
name = strings.ToLower(name)
name = validEmailOnlyValidCharacters.ReplaceAllString(name, "_")
return name
}

// User returns a fake user for Internet
func (i Internet) User() string {
user := i.Faker.RandomStringElement(userFormats)

p := i.Faker.Person()

// {{firstName}}
user = strings.Replace(user, "{{firstName}}", i.transformIntoValidEmailName(p.FirstName()), 1)
var (
cacheInternetUserFirstName []string
cacheInternetUserLastName []string
cacheInternetUserOnce sync.Once
cacheInternetUserFunc = func() {
cacheFirstNamesOnce.Do(cacheFirstNamesFunc)
for _, fn := range cacheFirstNames {
cacheInternetUserFirstName = append(cacheInternetUserFirstName, transformIntoValidEmailName(fn))
}

// {{lastName}}
user = strings.Replace(user, "{{lastName}}", i.transformIntoValidEmailName(p.LastName()), 1)
for _, ln := range lastName {
cacheInternetUserLastName = append(cacheInternetUserLastName, transformIntoValidEmailName(ln))
}
}
)

return user
// User returns a fake user for Internet
func (i Internet) User() string {
cacheInternetUserOnce.Do(cacheInternetUserFunc)

variant := i.Faker.IntBetween(0, 3)
switch variant {
case 0:
ln := i.Faker.RandomStringElement(cacheInternetUserLastName)
fn := i.Faker.RandomStringElement(cacheInternetUserFirstName)
return ln + "." + fn
case 1:
fn := i.Faker.RandomStringElement(cacheInternetUserFirstName)
ln := i.Faker.RandomStringElement(cacheInternetUserLastName)
return fn + "." + ln
case 2:
fn := i.Faker.RandomStringElement(cacheInternetUserFirstName)
return fn
case 3:
ln := i.Faker.RandomStringElement(cacheInternetUserLastName)
return ln
default:
panic("bad")
}
}

// Password returns a fake password for Internet
Expand Down Expand Up @@ -93,7 +113,7 @@ func (i Internet) Email() string {
email := i.Faker.RandomStringElement(emailFormats)

// {{user}}
email = strings.Replace(email, "{{user}}", i.transformIntoValidEmailName(i.User()), 1)
email = strings.Replace(email, "{{user}}", transformIntoValidEmailName(i.User()), 1)

// {{domain}}
email = strings.Replace(email, "{{domain}}", i.Domain(), 1)
Expand All @@ -108,23 +128,23 @@ func (i Internet) Email() string {
func (i Internet) FreeEmail() string {
domain := i.Faker.RandomStringElement(freeEmailDomain)

return i.transformIntoValidEmailName(i.User()) + "@" + domain
return transformIntoValidEmailName(i.User()) + "@" + domain
}

// SafeEmail returns a fake safe email address for Internet
func (i Internet) SafeEmail() string {
return i.transformIntoValidEmailName(i.User()) + "@" + i.SafeEmailDomain()
return transformIntoValidEmailName(i.User()) + "@" + i.SafeEmailDomain()
}

// CompanyEmail returns a fake company email address for Internet
func (i Internet) CompanyEmail() string {
c := i.Faker.Company()

companyName := i.transformIntoValidEmailName(c.Name())
companyName := transformIntoValidEmailName(c.Name())

domain := companyName + "." + i.Domain()

return i.transformIntoValidEmailName(i.User()) + "@" + domain
return transformIntoValidEmailName(i.User()) + "@" + domain
}

// TLD returns a fake tld for Internet
Expand Down

0 comments on commit e792f3e

Please sign in to comment.