-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodenames.go
38 lines (33 loc) · 959 Bytes
/
codenames.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
package codenames
import "strings"
// Part is any function that can return a string. This will usually
// be some kind of dictionary lookup that returns a random word.
type Part func() string
// New a generic syntaxically correct slug seperated by dashes. Returns
// a descriptive adjective, size, color, noun combo.
// Example: scared-pocket-pink-data
func New() string {
return Generate("-", nil, Descriptive, Size, Color, Noun)
}
// Generate creates a string with non-duplicated word parts seperated by a given value. Specific words may be
// excluded by placing them in the filter slice.
func Generate(separator string, filter []string, parts ...Part) string {
var out []string
for _, part := range parts {
set:
p := part()
for _, t := range filter {
if t == p {
goto set
}
}
for _, o := range out {
if o == p {
goto set
}
}
out = append(out, p)
}
formatted := strings.Join(out, separator)
return formatted
}