-
Notifications
You must be signed in to change notification settings - Fork 5
/
generator.go
142 lines (120 loc) · 2.88 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package fname
import (
"fmt"
"math/rand"
"strings"
"time"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type Casing int
const (
Lower Casing = iota
Upper
Title
)
func (c Casing) String() string {
switch c {
case Lower:
return "lower"
case Upper:
return "upper"
case Title:
return "title"
default:
return "unknown"
}
}
func CasingFromString(casing string) (Casing, error) {
switch strings.ToLower(casing) {
case Lower.String():
return Lower, nil
case Upper.String():
return Upper, nil
case Title.String():
return Title, nil
default:
return -1, fmt.Errorf("invalid casing: %s", casing)
}
}
type Generator struct {
casing Casing
dict *Dictionary
delimiter string
rand *rand.Rand
size uint
}
// GeneratorOption is a function that configures a Generator.
type GeneratorOption func(*Generator)
// WithCasing sets the casing used to format the generated name.
func WithCasing(casing Casing) GeneratorOption {
return func(g *Generator) {
g.casing = casing
}
}
// WithDelimiter sets the delimiter used to join words.
func WithDelimiter(delimiter string) GeneratorOption {
return func(g *Generator) {
g.delimiter = delimiter
}
}
// WithSeed sets the seed used to generate random numbers.
func WithSeed(seed int64) GeneratorOption {
return func(g *Generator) {
g.rand = rand.New(rand.NewSource(seed))
}
}
// WithSize sets the number of words in the generated name.
func WithSize(size uint) GeneratorOption {
return func(g *Generator) {
g.size = size
}
}
// NewGenerator creates a new Generator.
func NewGenerator(opts ...GeneratorOption) *Generator {
g := &Generator{
casing: Lower,
dict: NewDictionary(),
delimiter: "-",
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
size: 2,
}
for _, opt := range opts {
opt(g)
}
return g
}
// Generate generates a random name.
func (g *Generator) Generate() (string, error) {
if g.size < 2 || g.size > 4 {
return "", fmt.Errorf("invalid size: %d", g.size)
}
words := make([]string, 0, g.size)
adjectiveIndex := g.rand.Intn(g.dict.LengthAdjective())
nounIndex := g.rand.Intn(g.dict.LengthNoun())
for adjectiveIndex == nounIndex {
nounIndex = g.rand.Intn(g.dict.LengthNoun())
}
words = append(words, g.dict.adjectives[adjectiveIndex], g.dict.nouns[nounIndex])
if g.size >= 3 {
words = append(words, g.dict.verbs[g.rand.Intn(g.dict.LengthVerb())])
}
if g.size == 4 {
words = append(words, g.dict.adverbs[g.rand.Intn(g.dict.LengthAdverb())])
}
return strings.Join(g.applyCasing(words), g.delimiter), nil
}
func (g *Generator) applyCasing(words []string) []string {
if fn, ok := casingMap[g.casing]; ok {
for i, word := range words {
words[i] = fn(word)
}
}
return words
}
var titleCaser = cases.Title(language.English)
var casingMap = map[Casing]func(string) string{
Lower: strings.ToLower,
Upper: strings.ToUpper,
Title: titleCaser.String,
}