From 823c7e8bf7d3124fff32df8b28567524993663e9 Mon Sep 17 00:00:00 2001 From: Layla Date: Sat, 10 Jun 2023 08:24:17 +0000 Subject: [PATCH] Handle special characters in event names --- core/channel.go | 4 ++++ core/channel_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 core/channel_test.go diff --git a/core/channel.go b/core/channel.go index 7e8cb59..ec4dc61 100644 --- a/core/channel.go +++ b/core/channel.go @@ -22,6 +22,10 @@ func GenerateEventChannelName(eventName string, location string, dateTime time.T city := GetCityFromLocation(location) year := dateTime.Year() + // Remove special characters + eventName = regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(eventName, "") + eventName = strings.Trim(eventName, " ") + channel := fmt.Sprint(month, "-", day, city, "-", eventName, "-", year) channel = strings.ReplaceAll(channel, " ", "-") channel = strings.ToLower(channel) diff --git a/core/channel_test.go b/core/channel_test.go new file mode 100644 index 0000000..18cee1d --- /dev/null +++ b/core/channel_test.go @@ -0,0 +1,32 @@ +package core + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestGenerateEventChannelName(t *testing.T) { + assert := assert.New(t) + + // Test Valid Address + channelName := GenerateEventChannelName("Hello World", "1234 Place Rd, Ann Arbor, MI 00000", time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC)) + assert.Equal("jan-5-ann-arbor-hello-world-2022", channelName) + + // Test Unparsable + // lmanley: Note it'd be nice to expand support for this + channelName = GenerateEventChannelName("Hello World", "Michigan Theater, Ann Arbor", time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC)) + assert.Equal("jan-5-hello-world-2022", channelName) + + // Test Short Location + channelName = GenerateEventChannelName("Hello World", "Monroe, MI", time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC)) + assert.Equal("jan-5-monroe-hello-world-2022", channelName) + + // Test Remote Event + channelName = GenerateEventChannelName("Hello World", RemoteLocation, time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC)) + assert.Equal("jan-5-online-hello-world-2022", channelName) + + channelName = GenerateEventChannelName("Hangout :)", "Quickly Livonia", time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC)) + assert.Equal("jan-5-quickly-livonia-hangout-2022", channelName) +} -- 2.51.1