-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.go
58 lines (48 loc) · 987 Bytes
/
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
package namegen
import (
"math/rand"
"strings"
"github.com/samber/lo"
)
type Generator struct {
Capitalize bool
Alliterate bool
Separator string
}
func New() *Generator {
return &Generator{
Capitalize: true,
Alliterate: false,
Separator: " ",
}
}
func (g *Generator) Generate() string {
noun := Data.Nouns[rand.Intn(len(Data.Nouns))]
var adjectives []string
if g.Alliterate {
adjectives = lo.Filter(Data.Adjectives, func(adjective string, idx int) bool {
return strings.HasPrefix(adjective, noun[0:1])
})
if len(adjectives) == 0 {
adjectives = Data.Adjectives
}
} else {
adjectives = Data.Adjectives
}
res := strings.ReplaceAll(
strings.Join([]string{
adjectives[rand.Intn(len(adjectives))],
noun,
}, " "),
" ",
g.Separator,
)
if g.Capitalize {
res = strings.Title(res)
}
return res
}
// Generate returns a random name, equivalent to calling namegen.New().Generate()
func Generate() string {
return New().Generate()
}