-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
50 lines (43 loc) · 1.12 KB
/
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
package slug
type Options struct {
MaxLen int
Replacement string
RunesToRemove []rune
WholeWords bool
Unique bool
UniqueAttempts int
}
const (
optionsDefaultMaxLen = 128
optionsDefaultReplacement = "-"
optionsDefaultWholeWords = true
optionsDefaultUnique = false
optionsDefaultUniqueAttempts = 1 << 16
)
func optionsDefaultRunesToRemove() []rune {
return []rune{
'\'',
}
}
func NewOptionsWithDefaults() *Options {
return &Options{
MaxLen: optionsDefaultMaxLen,
Replacement: optionsDefaultReplacement,
RunesToRemove: optionsDefaultRunesToRemove(),
WholeWords: optionsDefaultWholeWords,
Unique: optionsDefaultUnique,
UniqueAttempts: optionsDefaultUniqueAttempts,
}
}
func initOptions(options *Options) *Options {
if options == nil {
options = NewOptionsWithDefaults()
}
if replacement := options.Replacement; replacement != optionsDefaultReplacement {
if replacement == "" || formatWordAcceptedRuneRegexp.MatchString(replacement) {
replacement = optionsDefaultReplacement
options.Replacement = replacement
}
}
return options
}