diff --git a/pkg/parser/callsign.go b/pkg/parser/callsign.go index f53a1be..8567e31 100644 --- a/pkg/parser/callsign.go +++ b/pkg/parser/callsign.go @@ -1,6 +1,7 @@ package parser import ( + "regexp" "strings" "unicode" ) @@ -9,8 +10,11 @@ import ( // - A single word, followed by a number consisting of any digits // - A number consisting of up to 3 digits // -// Garbage in between the digits is ignored. The result is normalized so that each digit is lowercase and space-delimited. +// Garbage in between the digits is ignored. Clan tags in the format "[CLAN]" +// are also ignored. The result is normalized so that each digit is lowercase +// and space-delimited. func ParsePilotCallsign(tx string) (callsign string, isValid bool) { + tx = removeClanTags(tx) tx = normalize(tx) tx = spaceDigits(tx) for token, replacement := range map[string]string{ @@ -45,3 +49,9 @@ func ParsePilotCallsign(tx string) (callsign string, isValid bool) { return callsign, true } + +var clanTagExpression = regexp.MustCompile(`\[.*?\]`) + +func removeClanTags(tx string) string { + return clanTagExpression.ReplaceAllString(tx, "") +} diff --git a/pkg/parser/callsign_test.go b/pkg/parser/callsign_test.go index f6bc313..e881202 100644 --- a/pkg/parser/callsign_test.go +++ b/pkg/parser/callsign_test.go @@ -1,10 +1,10 @@ package parser import ( + "strconv" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestParsePilotCallsign(t *testing.T) { @@ -25,14 +25,35 @@ func TestParsePilotCallsign(t *testing.T) { {"Red 054", "red 0 5 4"}, {"Gunfighter request", "gunfighter"}, {"This is Red 7", "red 7"}, + {"[CLAN] Wolf 1", "wolf 1"}, + {"Wolf 1 [CLAN]", "wolf 1"}, + {"[CLAN] Wolf 1 [1SG]", "wolf 1"}, + {"[Wolf 1", "wolf 1"}, } for _, test := range testCases { t.Run(test.name, func(t *testing.T) { t.Parallel() actual, ok := ParsePilotCallsign(test.name) - require.True(t, ok) + assert.True(t, ok) assert.Equal(t, test.expected, actual) }) } } + +func TestParsePilotCallsignInvalid(t *testing.T) { + t.Parallel() + testCases := []string{ + "", + "[]", + "[CLAN]", + } + + for i, test := range testCases { + t.Run(strconv.Itoa(i), func(t *testing.T) { + t.Parallel() + _, ok := ParsePilotCallsign(test) + assert.False(t, ok) + }) + } +}