Skip to content

Commit

Permalink
Finish up phone generator
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapette committed Nov 18, 2022
1 parent 422d21c commit 3e2a8cc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
33 changes: 30 additions & 3 deletions pkg/fakedata/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,29 @@ func enum(options string) (func() string, error) {
return withList(list), nil
}

func localPhone(options string) (func() string, error) {
numDigits := 8
numDigits, err := strconv.Atoi(options)
if err != nil {
return nil, err
}

switch numDigits {
case 8:
return integer("10000000,99999999")
case 9:
return integer("100000000,999999999")
case 10:
return integer("1000000000,9999999999")
case 11:
return integer("10000000000,99999999999")
case 12:
return integer("100000000000,999999999999")
default:
return nil, fmt.Errorf("digits must be >=8 and <=12")
}
}

func timestamp() func() string {
now := time.Now()
return func() string {
Expand Down Expand Up @@ -275,9 +298,8 @@ func uuidv7() string {
return u7.String()
}

var localPhone, _ = integer("10000000,9999999999")

func phoneGenerator(phoneCodeFunc func() string) func() string {
var localPhone, _ = integer("10000000,9999999999")
return func() string {
number := "+" + phoneCodeFunc() + localPhone()
if len(number) > 15 {
Expand Down Expand Up @@ -345,7 +367,6 @@ func newFactory() (f factory) {

generators.addGen(Generator{Name: "phone", Desc: "Phone number according to E.164", Func: phone})
generators.addGen(Generator{Name: "phone.code", Desc: "Calling country code", Func: phoneCode})
generators.addGen(Generator{Name: "phone.local", Desc: "Phone number without calling country code", Func: localPhone})

generators.addGen(Generator{Name: "state", Desc: "Full US state name", Func: withList(data.States)})

Expand Down Expand Up @@ -456,6 +477,12 @@ func newFactory() (f factory) {
CustomFunc: file,
})

generators.addGen(Generator{
Name: "phone.local",
Desc: "phone number without calling country code. It accepts an integer N number of digits. Min: 8, Max: 12",
CustomFunc: localPhone,
})

generators.addGen(Generator{Name: "uuidv1", Desc: "uuidv1", Func: uuidv1})
generators.addGen(Generator{Name: "uuidv4", Desc: "uuidv4", Func: uuidv4})
generators.addGen(Generator{Name: "uuidv6", Desc: "uuidv6", Func: uuidv6})
Expand Down
13 changes: 13 additions & 0 deletions pkg/fakedata/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ func BenchmarkInt(b *testing.B) {
}
}

func BenchmarkPhoneLocal(b *testing.B) {
phoneLocal := gens.FindByName("phone.local")

phoneLocalFunc, err := phoneLocal.CustomFunc("11")
if err != nil {
b.Fatalf("cannot create phone.local: %s", err)
}

for i := 0; i < b.N; i++ {
phoneLocalFunc()
}
}

func BenchmarkFile(b *testing.B) {
file := gens.FindByName("file")

Expand Down

0 comments on commit 3e2a8cc

Please sign in to comment.