Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions countries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) ||
Expand Down
40 changes: 40 additions & 0 deletions countries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading