Skip to content

Commit

Permalink
Ignore clan tags in pilot callsigns
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmab committed Feb 4, 2025
1 parent 44e9ff5 commit 744a8f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
12 changes: 11 additions & 1 deletion pkg/parser/callsign.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"regexp"
"strings"
"unicode"
)
Expand All @@ -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{
Expand Down Expand Up @@ -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, "")
}
25 changes: 23 additions & 2 deletions pkg/parser/callsign_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package parser

import (
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParsePilotCallsign(t *testing.T) {
Expand All @@ -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)
})
}
}

0 comments on commit 744a8f2

Please sign in to comment.