Skip to content

Commit

Permalink
Add a country list auto-generated from CLDR.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Oct 15, 2020
1 parent b0ae210 commit 9058f48
Show file tree
Hide file tree
Showing 4 changed files with 541 additions and 1 deletion.
31 changes: 30 additions & 1 deletion address.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package address

import "regexp"
import (
"regexp"
"sort"
)

// Address represents an address.
type Address struct {
Expand Down Expand Up @@ -81,6 +84,32 @@ func (f Format) CheckPostalCode(postalCode string) bool {
return rx.MatchString(postalCode)
}

// CheckCountryCode checks whether the given country code is valid.
//
// An empty country code is considered valid.
func CheckCountryCode(countryCode string) bool {
if countryCode == "" {
return true
}
_, ok := countries[countryCode]
return ok
}

// GetCountryCodes returns all known country codes.
func GetCountryCodes() []string {
countryCodes := make([]string, 0, len(countries))
for countryCode := range countries {
countryCodes = append(countryCodes, countryCode)
}
sort.Strings(countryCodes)
return countryCodes
}

// GetCountryNames returns all known country names, keyed by country code.
func GetCountryNames() map[string]string {
return countries
}

// GetFormats returns all known address formats, keyed by country code.
//
// The ZZ address format represents the generic fallback.
Expand Down
58 changes: 58 additions & 0 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,55 @@ func TestFormat_CheckPostalCode(t *testing.T) {
}
}

func TestCheckCountryCode(t *testing.T) {
tests := []struct {
countryCode string
want bool
}{
// Empty value.
{"", true},
// Valid country code.
{"FR", true},
// Invalid country code.
{"ABCD", false},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
got := address.CheckCountryCode(tt.countryCode)
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}

func TestGetCountryCodes(t *testing.T) {
countryCodes := address.GetCountryCodes()
for _, countryCode := range []string{"FR", "RS", "US"} {
if !contains(countryCodes, countryCode) {
t.Errorf("no %v country code found.", countryCode)
}
}
}

func TestGetCountryNames(t *testing.T) {
got := address.GetCountryNames()
want := map[string]string{
"FR": "France",
"RS": "Serbia",
}
for wantCode, wantName := range want {
gotName, ok := got[wantCode]
if !ok {
t.Errorf("no %v country code found.", wantCode)
}
if gotName != wantName {
t.Errorf("got %v, want %v", gotName, wantName)
}
}
}

func TestGetFormat(t *testing.T) {
// Existing format.
got := address.GetFormat("RS")
Expand Down Expand Up @@ -159,3 +208,12 @@ func TestGetFormats(t *testing.T) {
}
}
}

func contains(a []string, x string) bool {
for _, v := range a {
if v == x {
return true
}
}
return false
}
266 changes: 266 additions & 0 deletions countries.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9058f48

Please sign in to comment.