Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internet: cache Internet.User() first/last names #141

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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