-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
52 lines (42 loc) · 913 Bytes
/
options.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
package fantasyname
import (
"fmt"
"math/rand"
)
type config struct {
RandIntN func(int) int
Dictionary map[rune][]fmt.Stringer
Collapse bool
}
func (c *config) validate() {
if c.RandIntN == nil {
c.RandIntN = rand.Intn
}
if c.Dictionary == nil {
c.Dictionary = symbolMap
}
}
// Option is a configuration func.
type Option func(*config)
// Collapse creates option for collapsing doubles in result.
func Collapse(v bool) Option {
return func(c *config) {
c.Collapse = v
}
}
// RandFn creates option with custom random func.
func RandFn(v func(int) int) Option {
return func(c *config) {
c.RandIntN = v
}
}
// Dictionary sets custom dictionary (that maps runes to possible strings) for generation.
func Dictionary(d map[rune][]string) Option {
return func(c *config) {
t := make(map[rune][]fmt.Stringer)
for k, v := range d {
t[k] = convert(v)
}
c.Dictionary = t
}
}