From 027c9e423482261931a1c07e72cd25b55fe882cd Mon Sep 17 00:00:00 2001 From: Christoph Walther Date: Tue, 31 Oct 2023 21:52:24 +0100 Subject: [PATCH] 23 fix only whitespaces + conditional attributes --- backend/service/functions/string.go | 12 ++++---- backend/service/functions/string_test.go | 33 ++++++++++++++++++++++ backend/service/ical/icalFileGeneration.go | 16 +++++++++-- 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 backend/service/functions/string_test.go 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 eb4e2e7..151ed7c 100644 --- a/backend/service/ical/icalFileGeneration.go +++ b/backend/service/ical/icalFileGeneration.go @@ -33,14 +33,16 @@ func (icalModel IcalModel) EmitICal() goics.Componenter { s.AddProperty(k, v) k, v = goics.FormatDateTime("DTSTART;TZID=Europe/Berlin", event.Start.Time().Local().In(europeTime)) s.AddProperty(k, v) - s.AddProperty("SUMMARY", replaceNameIfUserDefined(&event, icalModel.Mapping)) - s.AddProperty("DESCRIPTION", generateDescription(event)) - s.AddProperty("LOCATION", event.Rooms) + addPropertyIfNotEmpty(s, "SUMMARY", replaceNameIfUserDefined(&event, icalModel.Mapping)) + addPropertyIfNotEmpty(s, "DESCRIPTION", generateDescription(event)) + addPropertyIfNotEmpty(s, "LOCATION", event.Rooms) c.AddComponent(s) } return c } +// 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 { for _, mapEntry := range mapping { if mapEntry.Name == event.Name && !functions.OnlyWhitespace(mapEntry.UserDefinedName) { @@ -50,6 +52,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