diff --git a/backend/service/functions/string.go b/backend/service/functions/string.go index f89ac44..f80b684 100644 --- a/backend/service/functions/string.go +++ b/backend/service/functions/string.go @@ -1,15 +1,13 @@ package functions -import "unicode" +import "strings" -// check if course is empty or contains only whitespaces +// check if string is empty or contains only whitespaces func OnlyWhitespace(word string) bool { - for _, letter := range word { - if !unicode.IsSpace(letter) || !(letter == int32(160)) { - return false - } + if len(strings.TrimSpace(word)) == 0 { + return true } - return true + return false } func Contains(s []string, e string) bool { diff --git a/backend/service/functions/string_test.go b/backend/service/functions/string_test.go new file mode 100644 index 0000000..16cfc9d --- /dev/null +++ b/backend/service/functions/string_test.go @@ -0,0 +1,33 @@ +package functions + +import "testing" + +func TestOnlyWhitespace(t *testing.T) { + type args struct { + word string + } + tests := []struct { + name string + args args + want bool + }{ + {"empty string", args{""}, true}, + {"whitespace", args{" "}, true}, + {"whitespaces", args{" "}, true}, + {"whitespaces and tabs", args{" \t"}, true}, + {"whitespaces and tabs and newlines", args{" \t\n"}, true}, + {"whitespaces and tabs and newlines and non-breaking spaces", args{" \t\n\u00A0"}, true}, + {"non-whitespace", args{"a"}, false}, + {"non-whitespaces", args{"abc"}, false}, + {"non-whitespaces and tabs", args{"abc\t"}, false}, + {"non-whitespaces and tabs and newlines", args{"abc\t\n"}, false}, + {"non-whitespaces and tabs and newlines and non-breaking spaces", args{"abc\t\n\u00A0"}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := OnlyWhitespace(tt.args.word); got != tt.want { + t.Errorf("OnlyWhitespace() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/backend/service/ical/icalFileGeneration.go b/backend/service/ical/icalFileGeneration.go index fa9e205..2c16380 100644 --- a/backend/service/ical/icalFileGeneration.go +++ b/backend/service/ical/icalFileGeneration.go @@ -37,19 +37,20 @@ func (icalModel IcalModel) EmitICal() goics.Componenter { s.AddProperty(k, v) if mappingFound { - s.AddProperty("SUMMARY", replaceNameIfUserDefined(&event, mapEntry)) + addPropertyIfNotEmpty(s, "SUMMARY", replaceNameIfUserDefined(&event, mapEntry)) addAlarmIfSpecified(s, event, mapEntry) } else { - s.AddProperty("SUMMARY", event.Name) + addPropertyIfNotEmpty(s, "SUMMARY", event.Name) } - s.AddProperty("DESCRIPTION", generateDescription(event)) - s.AddProperty("LOCATION", event.Rooms) + addPropertyIfNotEmpty(s, "DESCRIPTION", generateDescription(event)) + addPropertyIfNotEmpty(s, "LOCATION", event.Rooms) c.AddComponent(s) } return c } +// if reminder is specified in the configuration for this event, an alarm will be added to the event func addAlarmIfSpecified(s *goics.Component, event model.Event, mapping model.FeedCollection) { if mapping.Reminder { a := goics.NewComponent() @@ -61,6 +62,8 @@ func addAlarmIfSpecified(s *goics.Component, event model.Event, mapping model.Fe } } +// replaceNameIfUserDefined replaces the name of the event with the user defined name if it is not empty +// all contained template strings will be replaced with the corresponding values from the event func replaceNameIfUserDefined(event *model.Event, mapping model.FeedCollection) string { if !functions.OnlyWhitespace(mapping.UserDefinedName) { return names.ReplaceTemplateSubStrings(mapping.UserDefinedName, *event) @@ -69,6 +72,14 @@ func replaceNameIfUserDefined(event *model.Event, mapping model.FeedCollection) return event.Name } +// AddPropertyIfNotEmpty adds a property to the component if the value is not empty +// or contains only whitespaces +func addPropertyIfNotEmpty(component *goics.Component, key string, value string) { + if !functions.OnlyWhitespace(value) { + component.AddProperty(key, value) + } +} + func generateDescription(event model.Event) string { var description string