55 lines
910 B
Go
55 lines
910 B
Go
// Copyright 2023 Michael Amann and contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package utils
|
|
|
|
import "strings"
|
|
|
|
func StringToBool(s string) bool {
|
|
i := strings.ToLower(strings.TrimSpace(s))
|
|
if i == "yes" || i == "true" || i == "y" {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
func StringIsSet(s string) bool {
|
|
return strings.TrimSpace(s) != ""
|
|
}
|
|
|
|
func ValOrDefaultWBool(v, d string) (bool, string) {
|
|
if StringIsSet(v) {
|
|
return true, v
|
|
}
|
|
return false, d
|
|
}
|
|
|
|
func ValOrDefault(v, d string) string {
|
|
if StringIsSet(v) {
|
|
return v
|
|
}
|
|
return d
|
|
}
|
|
|
|
func IsDefault(v, d string) bool {
|
|
return strings.TrimSpace(v) == strings.TrimSpace(d)
|
|
}
|
|
|
|
func StringIfNotDefault(v, d string) string {
|
|
if IsDefault(v, d) {
|
|
return ""
|
|
}
|
|
return v
|
|
}
|
|
|
|
func StringSlug(s string, n int) (bool, string) {
|
|
if StringIsSet(s) {
|
|
if len(s) < n {
|
|
return false, s
|
|
}
|
|
return true, s[:n]
|
|
}
|
|
return false, ""
|
|
}
|