diff --git a/internal/parsers/schedule_parser.go b/internal/parsers/schedule_parser.go new file mode 100644 index 0000000..919a03d --- /dev/null +++ b/internal/parsers/schedule_parser.go @@ -0,0 +1,31 @@ +package parsers + +import "regexp" + +const ( + // DepartureCode = "dep" + // DepartureTime = "dep_time" + // ArrivalCode = "arr" + AirportCode = "airport" + WaypointPattern = `(?P\d{4}(\(\d{2}\w{3}\))?)(?P\w{3})\/?(?P\d{4}(\(\d{2}\w{3}\))?)` +) + +var ( + WaypointExpression = regexp.MustCompile(WaypointPattern) +) + +func FindWaypoints(message string) map[string]string { + matches := WaypointExpression.FindStringSubmatch(message) + if matches == nil { + return nil + } + + result := make(map[string]string) + for i, name := range WaypointExpression.SubexpNames() { + if i != 0 && name != "" { + result[name] = matches[i] + } + } + + return result +} diff --git a/internal/parsers/schedule_parser_test.go b/internal/parsers/schedule_parser_test.go new file mode 100644 index 0000000..5a7946f --- /dev/null +++ b/internal/parsers/schedule_parser_test.go @@ -0,0 +1,26 @@ +package parsers + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Schedule Parser", func() { + + Describe("FindWaypoints", func() { + It("should return the correct waypoints based on the message", func() { + message := "1845(11JUN)TSN/2100" + waypoints := FindWaypoints(message) + Expect(waypoints).NotTo(BeNil()) + Expect(waypoints[ArrivalTime]).To(Equal("1845(11JUN)")) + Expect(waypoints[AirportCode]).To(Equal("TSN")) + Expect(waypoints[DepartureTime]).To(Equal("2100")) + }) + + It("should return nil if no waypoints are found", func() { + message := "1845TSN" + waypoints := FindWaypoints(message) + Expect(waypoints).To(BeNil()) + }) + }) +})