Files
htwkalender-pwa/backend/service/functions/string.go
2024-01-24 02:07:30 +01:00

54 lines
1022 B
Go

package functions
import (
"crypto/sha256"
"encoding/hex"
"strings"
)
// check if string is empty or contains only whitespaces
func OnlyWhitespace(word string) bool {
return len(strings.TrimSpace(word)) == 0
}
// return function to check if rune is a separator
func IsSeparator(separator []rune) func(rune) bool {
return func(character rune) bool {
for _, sep := range separator {
if sep == character {
return true
}
}
return false
}
}
func Contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func ReplaceEmptyString(word string, replacement string) string {
if OnlyWhitespace(word) {
return replacement
}
return word
}
func HashString(s string) string {
hash := sha256.New()
hash.Write([]byte(s))
hashInBytes := hash.Sum(nil)
return hex.EncodeToString(hashInBytes)
}
func SeperateRoomString(rooms string) []string {
return strings.FieldsFunc(rooms, IsSeparator(
[]rune{',', '\t', '\n', '\r', ';', ' ', '\u00A0'}),
)
}