mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-02 17:59:14 +02:00
87 string splitting by separator, not whitespace
This commit is contained in:
@@ -27,7 +27,9 @@ func GetRooms(app *pocketbase.PocketBase) []string {
|
||||
var roomArray []string
|
||||
|
||||
for _, event := range events {
|
||||
var room = strings.Split(event.Rooms, " ")
|
||||
var room = strings.FieldsFunc(event.Rooms, functions.IsSeparator(
|
||||
[]rune{',', ' ', '\t', '\n', '\r', '\u00A0'},
|
||||
))
|
||||
//split functions room by space and add each room to array if it is not already in there
|
||||
for _, r := range room {
|
||||
var text = strings.TrimSpace(r)
|
||||
|
@@ -8,10 +8,19 @@ import (
|
||||
|
||||
// check if string is empty or contains only whitespaces
|
||||
func OnlyWhitespace(word string) bool {
|
||||
if len(strings.TrimSpace(word)) == 0 {
|
||||
return true
|
||||
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
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Contains(s []string, e string) bool {
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package functions
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOnlyWhitespace(t *testing.T) {
|
||||
type args struct {
|
||||
@@ -52,3 +54,29 @@ func TestHashString(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsSeparator(t *testing.T) {
|
||||
type args struct {
|
||||
separator []rune
|
||||
character rune
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{"empty separator", args{[]rune{}, 'a'}, false},
|
||||
{"separator with one rune equal", args{[]rune{'a'}, 'a'}, true},
|
||||
{"separator with one rune different", args{[]rune{'a'}, 'b'}, false},
|
||||
{"separator with two runes equal", args{[]rune{'a', 'b'}, 'a'}, true},
|
||||
{"separator with two runes equal second", args{[]rune{'a', 'b'}, 'b'}, true},
|
||||
{"separator with two runes different", args{[]rune{'a', 'b'}, 'c'}, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsSeparator(tt.args.separator)(tt.args.character); got != tt.want {
|
||||
t.Errorf("IsSeparator()() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user