2022-04-09 19:37:44 +03:00
|
|
|
package util
|
|
|
|
|
2022-04-21 17:25:35 +03:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"strings"
|
|
|
|
)
|
2022-04-09 19:37:44 +03:00
|
|
|
|
|
|
|
func Keys[K comparable, V any](input map[K]V) []K {
|
|
|
|
keys := make([]K, 0, len(input))
|
|
|
|
for k := range input {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
func Values[K comparable, V any](input map[K]V) []V {
|
|
|
|
values := make([]V, 0, len(input))
|
|
|
|
for _, v := range input {
|
|
|
|
values = append(values, v)
|
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContainsAny(s string, substrings ...string) bool {
|
|
|
|
for _, substring := range substrings {
|
|
|
|
if strings.Contains(s, substring) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-04-21 17:25:35 +03:00
|
|
|
|
|
|
|
func Title(input string) string {
|
|
|
|
words := strings.Split(input, " ")
|
|
|
|
smallwords := " a an on the to of "
|
|
|
|
|
|
|
|
for index, word := range words {
|
|
|
|
if strings.Contains(smallwords, " "+word+" ") && index > 0 {
|
|
|
|
words[index] = word
|
|
|
|
} else {
|
|
|
|
words[index] = strings.Title(word)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(words, " ")
|
|
|
|
}
|
|
|
|
|
2022-04-21 23:01:18 +03:00
|
|
|
func Capitalize(input string) string {
|
|
|
|
return strings.ToUpper(input[:1]) + input[1:]
|
|
|
|
}
|
|
|
|
|
2022-04-21 17:25:35 +03:00
|
|
|
func Json(obj any) template.HTML {
|
|
|
|
b, err := json.MarshalIndent(obj, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return template.HTML(`<span class="json">` + string(b) + `</span>`)
|
|
|
|
}
|
2022-04-21 23:01:18 +03:00
|
|
|
|
|
|
|
func If[T any](cond bool, v1, v2 T) T {
|
|
|
|
if cond {
|
|
|
|
return v1
|
|
|
|
} else {
|
|
|
|
return v2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Map[U, V any](list []U, mapper func(U) V) []V {
|
|
|
|
var newList = make([]V, 0, len(list))
|
|
|
|
for _, i := range list {
|
|
|
|
newList = append(newList, mapper(i))
|
|
|
|
}
|
|
|
|
return newList
|
|
|
|
}
|