From 35ea47eafa9e6c91e07bbc2b1e326991e9243803 Mon Sep 17 00:00:00 2001 From: dillon Date: Sat, 24 Dec 2022 19:44:25 -0600 Subject: [PATCH] remove intermediary options struct --- README.md | 4 ++-- example/main.go | 4 ++-- generator.go | 36 +++++++++++++++++++----------------- generator_test.go | 6 +++--- go.mod | 4 ++-- options.go | 25 +++++++++---------------- 6 files changed, 37 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index b00deb0..00f3712 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## Installation ```sh -go get github.com/DillonStreator/go-unique-name-generator +go get github.com/dillonstreator/go-unique-name-generator ``` ## Basic Usage @@ -15,7 +15,7 @@ package main import ( "fmt" - ung "github.com/DillonStreator/go-unique-name-generator" + ung "github.com/dillonstreator/go-unique-name-generator" ) func main() { diff --git a/example/main.go b/example/main.go index 8cb17ac..9f0ccda 100644 --- a/example/main.go +++ b/example/main.go @@ -4,8 +4,8 @@ import ( "fmt" "strings" - ung "github.com/DillonStreator/go-unique-name-generator" - "github.com/DillonStreator/go-unique-name-generator/dictionaries" + ung "github.com/dillonstreator/go-unique-name-generator" + "github.com/dillonstreator/go-unique-name-generator/dictionaries" ) func main() { diff --git a/generator.go b/generator.go index 64c800e..b84ed92 100644 --- a/generator.go +++ b/generator.go @@ -6,7 +6,7 @@ import ( "strings" "time" - "github.com/DillonStreator/go-unique-name-generator/dictionaries" + "github.com/dillonstreator/go-unique-name-generator/dictionaries" ) var alphaNumericReg = regexp.MustCompile("[^a-zA-Z0-9]+") @@ -17,14 +17,17 @@ func sanitizeString(str string) string { // UniqueNameGenerator is a unique name generator instance type UniqueNameGenerator struct { - options *options + separator string + dictionaries [][]string + style Style + sanitizer Sanitizer + + rnd *rand.Rand } // NewUniqueNameGenerator creates a new instance of UniqueNameGenerator func NewUniqueNameGenerator(opts ...option) *UniqueNameGenerator { - rand.Seed(time.Now().UTC().UnixNano()) - - _opts := &options{ + ung := &UniqueNameGenerator{ separator: "_", dictionaries: [][]string{ dictionaries.Adjectives, @@ -33,24 +36,23 @@ func NewUniqueNameGenerator(opts ...option) *UniqueNameGenerator { }, sanitizer: sanitizeString, style: Lower, + rnd: rand.New(rand.NewSource(time.Now().UTC().UnixNano())), } for _, opt := range opts { - opt(_opts) + opt(ung) } - return &UniqueNameGenerator{ - options: _opts, - } + return ung } // Generate generates a new unique name with the configuration func (ung *UniqueNameGenerator) Generate() string { - words := make([]string, len(ung.options.dictionaries)) - for i, dict := range ung.options.dictionaries { - randomIndex := rand.Intn(len(dict)) - word := ung.options.sanitizer(dict[randomIndex]) - switch ung.options.style { + words := make([]string, len(ung.dictionaries)) + for i, dict := range ung.dictionaries { + randomIndex := ung.rnd.Intn(len(dict)) + word := ung.sanitizer(dict[randomIndex]) + switch ung.style { case Lower: word = strings.ToLower(word) case Upper: @@ -60,16 +62,16 @@ func (ung *UniqueNameGenerator) Generate() string { } words[i] = word } - return strings.Join(words, ung.options.separator) + return strings.Join(words, ung.separator) } // UniquenessCount returns the number of unique combinations func (ung *UniqueNameGenerator) UniquenessCount() uint64 { - if len(ung.options.dictionaries) == 0 { + if len(ung.dictionaries) == 0 { return 0 } var count uint64 = 1 - for _, set := range ung.options.dictionaries { + for _, set := range ung.dictionaries { count *= uint64(len(set)) } return count diff --git a/generator_test.go b/generator_test.go index a865048..909b4c7 100644 --- a/generator_test.go +++ b/generator_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/DillonStreator/go-unique-name-generator/dictionaries" + "github.com/dillonstreator/go-unique-name-generator/dictionaries" ) func includes(items []string, str string, transform func(item string) string) bool { @@ -19,7 +19,7 @@ func includes(items []string, str string, transform func(item string) string) bo func TestNewUniqueNameGenerator(t *testing.T) { t.Run("should use correct default options", func(t *testing.T) { g := NewUniqueNameGenerator() - if len(g.options.dictionaries) != 3 { + if len(g.dictionaries) != 3 { t.Error("default generator should use 3 dictionaries") } word := g.Generate() @@ -46,7 +46,7 @@ func TestNewUniqueNameGenerator(t *testing.T) { dictionaries.Names, }), WithSeparator("-"), WithStyle(Upper), ) - if len(g.options.dictionaries) != 4 { + if len(g.dictionaries) != 4 { t.Error("should have 4 dictionaries") } word := g.Generate() diff --git a/go.mod b/go.mod index bdd6e5f..e17930b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/DillonStreator/go-unique-name-generator +module github.com/dillonstreator/go-unique-name-generator -go 1.18 +go 1.19 diff --git a/options.go b/options.go index 435ac84..315c74c 100644 --- a/options.go +++ b/options.go @@ -10,35 +10,28 @@ const ( Capital ) -type options struct { - separator string - dictionaries [][]string - style Style - sanitizer Sanitizer -} - -type option func(opts *options) +type option func(ung *UniqueNameGenerator) func WithSeparator(separator string) option { - return func(opts *options) { - opts.separator = separator + return func(ung *UniqueNameGenerator) { + ung.separator = separator } } func WithDictionaries(dictionaries [][]string) option { - return func(opts *options) { - opts.dictionaries = dictionaries + return func(ung *UniqueNameGenerator) { + ung.dictionaries = dictionaries } } func WithStyle(style Style) option { - return func(opts *options) { - opts.style = style + return func(ung *UniqueNameGenerator) { + ung.style = style } } func WithSanitizer(sanitizer Sanitizer) option { - return func(opts *options) { - opts.sanitizer = sanitizer + return func(ung *UniqueNameGenerator) { + ung.sanitizer = sanitizer } }