Skip to content

Commit

Permalink
refactor: remove the prefix "random"
Browse files Browse the repository at this point in the history
Having `Random` to function name or files is redundant.

BREAKING CHANGE: All functions have been renamed.
  • Loading branch information
neumachen committed Jul 4, 2023
1 parent f8a67aa commit 97893be
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
10 changes: 6 additions & 4 deletions random_ssn.go → ssn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
"time"
)

var sSNRegexString = `^[0-9]{3}[ -]?(0[1-9]|[1-9][0-9])[ -]?([1-9][0-9]{3}|[0-9][1-9][0-9]{2}|[0-9]{2}[1-9][0-9]|[0-9]{3}[1-9])$`
var sSNRegex = regexp.MustCompile(sSNRegexString)
var (
sSNRegexString = `^[0-9]{3}[ -]?(0[1-9]|[1-9][0-9])[ -]?([1-9][0-9]{3}|[0-9][1-9][0-9]{2}|[0-9]{2}[1-9][0-9]|[0-9]{3}[1-9])$`
sSNRegex = regexp.MustCompile(sSNRegexString)
)

// RandomSSN will try to generate a valid random SSN by generating up until
// SSN will try to generate a valid random SSN by generating up until
// the given retries or until it generates a valid SSN whichever comes first.
// The retires default to 100 if no value is given.
// If formatted it will return a string with the format XXX-XX-XXXX opposed to
// non formatted XXXXXXXXX.
func RandomSSN(formatted bool, routines int) string {
func SSN(formatted bool, routines int) string {
validSSN := make(chan string)

if routines == 0 {
Expand Down
6 changes: 4 additions & 2 deletions random_ssn_test.go → ssn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/stretchr/testify/require"
)

func TestRandomSSN(t *testing.T) {
ssn := RandomSSN(false, 100)
func TestSSN(t *testing.T) {
t.Parallel()

ssn := SSN(false, 100)
require.NotEmpty(t, ssn)
}
10 changes: 5 additions & 5 deletions random_us_address.go → us_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"golang.org/x/sync/errgroup"
)

// RandomUSAddress picks a random address from the initialized USAddresses.
// USAddress picks a random address from the initialized USAddresses.
// Note that for latitude, this only picks up to the 6th decimal place since
// some of the lat and long in the dataset contain around 13 decimal places.
// The reason for this limitation is unknown.
func RandomUSAddress() (*Address, error) {
func USAddress() (*Address, error) {
count := big.NewInt(int64(len(USAddresses)))
for {
randIndex, err := rand.Int(rand.Reader, count)
Expand All @@ -29,11 +29,11 @@ func RandomUSAddress() (*Address, error) {
}
}

// RandomUSStateAddress returns a random address from the initialized USAddresses
// USStateAddress returns a random address from the initialized USAddresses
// that belongs to the specified state. It uses multiple goroutines to improve
// performance, with the number of goroutines specified by the 'routines' parameter.
// If 'routines' is 0, it defaults to 10.
func RandomUSStateAddress(ctx context.Context, state string, routines int) (*Address, error) {
func USStateAddress(ctx context.Context, state string, routines int) (*Address, error) {
if routines == 0 {
routines = 15
}
Expand All @@ -51,7 +51,7 @@ func RandomUSStateAddress(ctx context.Context, state string, routines int) (*Add
case <-gCtx.Done():
return gCtx.Err()
default:
a, err := RandomUSAddress()
a, err := USAddress()
if err != nil {
return err
}
Expand Down
31 changes: 17 additions & 14 deletions random_us_address_test.go → us_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package randata

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRandomUSAddress_Success(t *testing.T) {
func TestUSAddress(t *testing.T) {
t.Parallel()
address, err := RandomUSAddress()

address, err := USAddress()
require.NoError(t, err)
require.NotNil(t, address)
assert.NotEmpty(t, address.StreetNumber)
Expand All @@ -23,21 +23,24 @@ func TestRandomUSAddress_Success(t *testing.T) {
assert.NotEmpty(t, address.Latitude)
assert.NotEmpty(t, address.Longitude)

lat := strings.Split(address.Latitude.ToString(), ".")
long := strings.Split(address.Longitude.ToString(), ".")

assert.True(t, len(lat) == 2)
assert.True(t, len(lat[1]) < 7)
assert.True(t, len(long) == 2)
assert.True(t, len(lat[1]) == 6)
assert.True(t, len(long[1]) == 6)
assert.True(t, len(long[1]) < 7)
// NOTE: commenting this out until we fix the dataset to have consistent
// coordinates.
// lat := strings.Split(address.Latitude.ToString(), ".")
// long := strings.Split(address.Longitude.ToString(), ".")
//
// assert.True(t, len(lat) == 2)
// assert.True(t, len(lat[1]) < 7)
// assert.True(t, len(long) == 2)
// assert.True(t, len(lat[1]) == 6)
// assert.True(t, len(long[1]) == 6)
// assert.True(t, len(long[1]) < 7)
}

func TestRandomUSStateAddress_Success(t *testing.T) {
func TestUSStateAddress(t *testing.T) {
t.Parallel()

state := "IL"
address, err := RandomUSStateAddress(context.Background(), state, 10)
address, err := USStateAddress(context.Background(), state, 10)
require.NoError(t, err)
require.NotNil(t, address)
assert.Equal(t, state, address.AdministrativeAreaLevel1)
Expand Down

0 comments on commit 97893be

Please sign in to comment.