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) || 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()