-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerator.go
More file actions
156 lines (134 loc) · 3.44 KB
/
generator.go
File metadata and controls
156 lines (134 loc) · 3.44 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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"
}
}
// ParseCasing parses a casing string and returns the corresponding Casing value.
func ParseCasing(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)
}
}
// Deprecated: Use ParseCasing instead.
func CasingFromString(casing string) (Casing, error) {
return ParseCasing(casing)
}
// Generator generates random name phrases. It is not safe for concurrent use
// from multiple goroutines; create a separate Generator per goroutine instead.
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
}
}
// WithDictionary sets a custom Dictionary on the Generator.
// If d is nil, the default embedded Dictionary is used.
func WithDictionary(d *Dictionary) GeneratorOption {
return func(g *Generator) {
if d != nil {
g.dict = d
}
}
}
// 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.
// Returns an error if size is outside the valid range [2, 4].
func WithSize(size uint) (GeneratorOption, error) {
if size < 2 || size > 4 {
return nil, fmt.Errorf("invalid size: %d", size)
}
return func(g *Generator) {
g.size = size
}, nil
}
// 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) {
words := make([]string, 0, g.size)
adjectiveIndex := g.rand.Intn(g.dict.LengthAdjective())
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
}
var titleCaser = cases.Title(language.English)
func (g *Generator) applyCasing(words []string) []string {
for i, word := range words {
switch g.casing {
case Lower:
words[i] = strings.ToLower(word)
case Upper:
words[i] = strings.ToUpper(word)
case Title:
words[i] = titleCaser.String(word)
}
}
return words
}