gask-old/helpers.go

79 lines
1.2 KiB
Go

package main
import (
"strconv"
"strings"
"unicode"
)
func itoa(val int) string {
return strconv.Itoa(val)
}
func atoi(val string) int {
return oatoi(val, -1)
}
func oatoi(val string, def int) int {
v, err := strconv.Atoi(val)
if err != nil {
return def
}
return v
}
func isCombination(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for _, v := range s1 {
var found bool
for _, vv := range s2 {
if v == vv {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// We split projects/contexts on spaces or commas
func splitFields(v string) []string {
f := func(c rune) bool {
return unicode.IsSpace(c) || c == ','
}
return strings.FieldsFunc(v, f)
}
func tagsToSlice(v map[string]string) []string {
var ret []string
for k, v := range v {
ret = append(ret, k+":"+v)
}
return ret
}
func sliceToTags(v []string) map[string]string {
ret := make(map[string]string)
for _, vv := range v {
tags := strings.Split(vv, ":")
if len(tags) == 2 {
ret[tags[0]] = tags[1]
}
}
return ret
}
func sliceIsValidTags(v []string) bool {
for _, vv := range v {
if len(strings.Split(vv, ":")) != 2 {
return false
}
}
return true
}