diff --git a/pkg/fakedata/generator.go b/pkg/fakedata/generator.go index 3a79860..bfc24dd 100644 --- a/pkg/fakedata/generator.go +++ b/pkg/fakedata/generator.go @@ -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 { @@ -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 { @@ -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)}) @@ -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}) diff --git a/pkg/fakedata/generator_test.go b/pkg/fakedata/generator_test.go index 03221ff..2a1b387 100644 --- a/pkg/fakedata/generator_test.go +++ b/pkg/fakedata/generator_test.go @@ -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")