Skip to content

Commit

Permalink
remove intermediary options struct
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonstreator committed Dec 25, 2022
1 parent 577d68c commit 35ea47e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
36 changes: 19 additions & 17 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]+")
Expand All @@ -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,
Expand All @@ -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:
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
25 changes: 9 additions & 16 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 35ea47e

Please sign in to comment.