@@ -31,7 +31,8 @@ func (c Casing) String() string {
3131 }
3232}
3333
34- func CasingFromString (casing string ) (Casing , error ) {
34+ // ParseCasing parses a casing string and returns the corresponding Casing value.
35+ func ParseCasing (casing string ) (Casing , error ) {
3536 switch strings .ToLower (casing ) {
3637 case Lower .String ():
3738 return Lower , nil
@@ -44,6 +45,13 @@ func CasingFromString(casing string) (Casing, error) {
4445 }
4546}
4647
48+ // Deprecated: Use ParseCasing instead.
49+ func CasingFromString (casing string ) (Casing , error ) {
50+ return ParseCasing (casing )
51+ }
52+
53+ // Generator generates random name phrases. It is not safe for concurrent use
54+ // from multiple goroutines; create a separate Generator per goroutine instead.
4755type Generator struct {
4856 casing Casing
4957 dict * Dictionary
@@ -69,6 +77,16 @@ func WithDelimiter(delimiter string) GeneratorOption {
6977 }
7078}
7179
80+ // WithDictionary sets a custom Dictionary on the Generator.
81+ // If d is nil, the default embedded Dictionary is used.
82+ func WithDictionary (d * Dictionary ) GeneratorOption {
83+ return func (g * Generator ) {
84+ if d != nil {
85+ g .dict = d
86+ }
87+ }
88+ }
89+
7290// WithSeed sets the seed used to generate random numbers.
7391func WithSeed (seed int64 ) GeneratorOption {
7492 return func (g * Generator ) {
@@ -77,10 +95,14 @@ func WithSeed(seed int64) GeneratorOption {
7795}
7896
7997// WithSize sets the number of words in the generated name.
80- func WithSize (size uint ) GeneratorOption {
98+ // Returns an error if size is outside the valid range [2, 4].
99+ func WithSize (size uint ) (GeneratorOption , error ) {
100+ if size < 2 || size > 4 {
101+ return nil , fmt .Errorf ("invalid size: %d" , size )
102+ }
81103 return func (g * Generator ) {
82104 g .size = size
83- }
105+ }, nil
84106}
85107
86108// NewGenerator creates a new Generator.
@@ -100,16 +122,9 @@ func NewGenerator(opts ...GeneratorOption) *Generator {
100122
101123// Generate generates a random name.
102124func (g * Generator ) Generate () (string , error ) {
103- if g .size < 2 || g .size > 4 {
104- return "" , fmt .Errorf ("invalid size: %d" , g .size )
105- }
106-
107125 words := make ([]string , 0 , g .size )
108126 adjectiveIndex := g .rand .Intn (g .dict .LengthAdjective ())
109127 nounIndex := g .rand .Intn (g .dict .LengthNoun ())
110- for adjectiveIndex == nounIndex {
111- nounIndex = g .rand .Intn (g .dict .LengthNoun ())
112- }
113128
114129 words = append (words , g .dict .adjectives [adjectiveIndex ], g .dict .nouns [nounIndex ])
115130
@@ -124,19 +139,18 @@ func (g *Generator) Generate() (string, error) {
124139 return strings .Join (g .applyCasing (words ), g .delimiter ), nil
125140}
126141
142+ var titleCaser = cases .Title (language .English )
143+
127144func (g * Generator ) applyCasing (words []string ) []string {
128- if fn , ok := casingMap [g .casing ]; ok {
129- for i , word := range words {
130- words [i ] = fn (word )
145+ for i , word := range words {
146+ switch g .casing {
147+ case Lower :
148+ words [i ] = strings .ToLower (word )
149+ case Upper :
150+ words [i ] = strings .ToUpper (word )
151+ case Title :
152+ words [i ] = titleCaser .String (word )
131153 }
132154 }
133155 return words
134156}
135-
136- var titleCaser = cases .Title (language .English )
137-
138- var casingMap = map [Casing ]func (string ) string {
139- Lower : strings .ToLower ,
140- Upper : strings .ToUpper ,
141- Title : titleCaser .String ,
142- }
0 commit comments