60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
// Dict allows to send more than one variable into a template
|
|
func Dict(values ...interface{}) (map[string]interface{}, error) {
|
|
if len(values)%2 != 0 {
|
|
return nil, errors.New("invalid dict call")
|
|
}
|
|
dict := make(map[string]interface{}, len(values)/2)
|
|
for i := 0; i < len(values); i += 2 {
|
|
key, ok := values[i].(string)
|
|
if !ok {
|
|
return nil, errors.New("dict keys must be strings")
|
|
}
|
|
dict[key] = values[i+1]
|
|
}
|
|
return dict, nil
|
|
}
|
|
|
|
func IsMap(sth interface{}) bool {
|
|
return reflect.ValueOf(sth).Kind() == reflect.Map
|
|
}
|
|
|
|
func IsSlice(sth interface{}) bool {
|
|
return reflect.ValueOf(sth).Kind() == reflect.Slice
|
|
}
|
|
|
|
func IsArray(sth interface{}) bool {
|
|
return reflect.ValueOf(sth).Kind() == reflect.Array
|
|
}
|
|
|
|
func SplitCapitalize(name string) string {
|
|
var words []string
|
|
l := 0
|
|
for s := name; s != ""; s = s[l:] {
|
|
l = strings.IndexFunc(s[1:], unicode.IsUpper) + 1
|
|
if l <= 0 {
|
|
l = len(s)
|
|
}
|
|
words = append(words, s[:l])
|
|
}
|
|
|
|
name = ""
|
|
|
|
for _, element := range words {
|
|
name += element + " "
|
|
}
|
|
|
|
name = strings.ToLower(name[:len(name)-1])
|
|
name = strings.ToUpper(string(name[0])) + name[1:len(name)]
|
|
|
|
return name
|
|
}
|