-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
69 lines (61 loc) · 1.56 KB
/
utils.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
59
60
61
62
63
64
65
66
67
68
69
package gropdown
import (
"fmt"
"strings"
)
func buttonId(label string) string {
return fmt.Sprintf("dropdown-button-%s", strings.ToLower(label))
}
func menuId(label string) string {
return fmt.Sprintf("dropdown-menu-%s", strings.ToLower(label))
}
// trimmed strips away '"' from a string.
func trimmed(s string) string {
return strings.Trim(s, "\"")
}
// toSlug returns a copy of string with lowercase
// replacing "_" and whitespaces with "-"
// example: toSlug("New Resource") returns new-resource.
func toSlug(s string) string {
return strings.Map(func(r rune) rune {
switch {
case r == ' ', r == '_':
return '-'
case r == '"':
return -1 // Remove quotes
default:
return r
}
}, strings.ToLower(trimmed(s)))
}
// getButtonIcon If icons are provided, use the first one; otherwise, empty ButtonIcon struct.
func getButtonIcon(icons []*ButtonIcon) *ButtonIcon {
var icon *ButtonIcon
if len(icons) > 0 {
icon = icons[0]
} else {
icon = &ButtonIcon{}
}
return icon
}
// getItemOptions retrieves the options for a dropdown item, only the first one is considered.
func getItemOptions(opts []ItemOptions) ItemOptions {
var opt ItemOptions
if len(opts) > 0 {
opt = opts[0]
} else {
opt = ItemOptions{}
}
return opt
}
// buildItemOptions creates a new DropdownItem with the given label and options.
func buildItemOptions(label string, options []ItemOptions) *DropdownItem {
opts := getItemOptions(options)
return &DropdownItem{
Label: label,
Icon: opts.Icon,
Href: opts.Href,
External: opts.External,
Attrs: opts.Attrs,
}
}