23 fix only whitespaces + conditional attributes

This commit is contained in:
Christoph Walther
2023-10-31 21:52:24 +01:00
parent 4e23903b31
commit 027c9e4234
3 changed files with 51 additions and 10 deletions

View File

@@ -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 {

View File

@@ -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)
}
})
}
}

View File

@@ -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