From b3d159d3f94e59d5c263299dca47fbbc3edd1f52 Mon Sep 17 00:00:00 2001 From: Nico Date: Thu, 30 Apr 2026 12:38:41 +0200 Subject: [PATCH 1/2] feat: add separate exists methods for alpha-2 and alpha-3 codes --- countries.go | 56 +++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/countries.go b/countries.go index af87305..b561ac5 100644 --- a/countries.go +++ b/countries.go @@ -14,20 +14,46 @@ func AllMappings() []Mapping { return mappings } -// Exists checks if any occurrence of the query matches. +// Exists checks if any occurrence of the query matches an ISO-3166-1 Alpha-2 code, an ISO-3166-1 Alpha-3 code or a country name. func Exists(query string) bool { switch { case len(query) < alpha2Len: return false case len(query) == alpha2Len: - return existsByAlpha2(query) + return ExistsAlpha2(query) case len(query) == alpha3Len: - return existsByAlpha3(query) + return ExistsAlpha3(query) default: return existsByNameOrNationality(query) } } +// ExistsAlpha2 checks if any occurrence of the query matches an ISO-3166-1 Alpha-2 code. +func ExistsAlpha2(query string) bool { + query = strings.ToUpper(query) + + for i := range mappings { + if mappings[i].Alpha2 == query { + return true + } + } + + return false +} + +// ExistsAlpha3 checks if any occurrence of the query matches an ISO-3166-1 Alpha-3 code. +func ExistsAlpha3(query string) bool { + query = strings.ToUpper(query) + + for i := range mappings { + if mappings[i].Alpha3 == query { + return true + } + } + + return false +} + // FindCountry looks up any matching occurrence of the query. func FindCountry(query string) (*Mapping, error) { switch { @@ -77,30 +103,6 @@ func findCountryByNameOrNationality(query string) (*Mapping, error) { return nil, ErrCountryNotFound } -func existsByAlpha2(query string) bool { - query = strings.ToUpper(query) - - for i := range mappings { - if mappings[i].Alpha2 == query { - return true - } - } - - return false -} - -func existsByAlpha3(query string) bool { - query = strings.ToUpper(query) - - for i := range mappings { - if mappings[i].Alpha3 == query { - return true - } - } - - return false -} - func existsByNameOrNationality(query string) bool { for i := range mappings { if isCountryNameOrNationality(mappings[i].Translations[EN], query) || From 7d410606f99319e56f43da23b13e1e7c8e62f237 Mon Sep 17 00:00:00 2001 From: Nico Date: Thu, 30 Apr 2026 12:39:32 +0200 Subject: [PATCH 2/2] test: add tests for exists alpha-2 alpha-3 methods --- countries_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/countries_test.go b/countries_test.go index ed5a171..052a2a5 100644 --- a/countries_test.go +++ b/countries_test.go @@ -59,6 +59,46 @@ func Test_Exists(t *testing.T) { }) } +func Test_ExistsAlpha2(t *testing.T) { + t.Run("exists alpha 2", func(t *testing.T) { + if !countries.ExistsAlpha2("AU") { + t.Error("expected: true, got: false") + } + }) + + t.Run("exists alpha 3", func(t *testing.T) { + if countries.ExistsAlpha2("AUS") { + t.Error("expected: false, got: true") + } + }) + + t.Run("does not exist", func(t *testing.T) { + if countries.ExistsAlpha2("NoAlpha2Code") { + t.Error("expected: false, got: true") + } + }) +} + +func Test_ExistsAlpha3(t *testing.T) { + t.Run("exists alpha 3", func(t *testing.T) { + if !countries.ExistsAlpha3("AUS") { + t.Error("expected: true, got: false") + } + }) + + t.Run("exists alpha 2", func(t *testing.T) { + if countries.ExistsAlpha3("AU") { + t.Error("expected: false, got: true") + } + }) + + t.Run("does not exist", func(t *testing.T) { + if countries.ExistsAlpha3("NoAlpha2Code") { + t.Error("expected: false, got: true") + } + }) +} + func BenchmarkGetByCountryName(b *testing.B) { b.ReportAllocs()