-
Notifications
You must be signed in to change notification settings - Fork 739
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kehan Pan <[email protected]>
- Loading branch information
Showing
50 changed files
with
4,072 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package countrycode | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type CountryCode struct { | ||
map2To3 map[string]string | ||
map3To2 map[string]string | ||
} | ||
|
||
func New() *CountryCode { | ||
return &CountryCode{ | ||
map2To3: make(map[string]string), | ||
map3To2: make(map[string]string), | ||
} | ||
} | ||
|
||
// Load loads country code mapping data | ||
func (c *CountryCode) Load(data string) { | ||
toAlpha2 := make(map[string]string) | ||
toAlpha3 := make(map[string]string) | ||
for _, line := range strings.Split(data, "\n") { | ||
if line == "" { | ||
continue | ||
} | ||
fields := strings.Split(line, ",") | ||
if len(fields) < 2 { | ||
continue | ||
} | ||
alpha2 := strings.TrimSpace(fields[0]) | ||
alpha3 := strings.TrimSpace(fields[1]) | ||
toAlpha2[alpha3] = alpha2 | ||
toAlpha3[alpha2] = alpha3 | ||
} | ||
|
||
c.map2To3 = toAlpha3 | ||
c.map3To2 = toAlpha2 | ||
} | ||
|
||
// ToAlpha3 converts country code alpha2 to alpha3 | ||
func (c *CountryCode) ToAlpha3(alpha2 string) string { | ||
return c.map2To3[alpha2] | ||
} | ||
|
||
// ToAlpha2 converts country code alpha3 to alpha2 | ||
func (c *CountryCode) ToAlpha2(alpha3 string) string { | ||
return c.map3To2[alpha3] | ||
} | ||
|
||
var defaultCountryCode = New() | ||
|
||
func Load(data string) { | ||
defaultCountryCode.Load(data) | ||
} | ||
|
||
func ToAlpha3(alpha2 string) string { | ||
return defaultCountryCode.ToAlpha3(alpha2) | ||
} | ||
|
||
func ToAlpha2(alpha3 string) string { | ||
return defaultCountryCode.ToAlpha2(alpha3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package countrycode | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCountryCode(t *testing.T) { | ||
Load(` | ||
AD,AND | ||
AE,ARE | ||
AF,AFG | ||
`) | ||
assert.Equal(t, "AND", ToAlpha3("AD"), "map AD to AND") | ||
assert.Equal(t, "AE", ToAlpha2("ARE"), "map ARE to AE") | ||
} | ||
|
||
func TestCountryCodeToAlpha3(t *testing.T) { | ||
c := New() | ||
c.Load(` | ||
AD,AND | ||
AE,ARE | ||
AF,AFG | ||
`) | ||
tests := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"AD", "AND"}, | ||
{"AE", "ARE"}, | ||
{"XX", ""}, | ||
{"", ""}, | ||
} | ||
|
||
for _, test := range tests { | ||
assert.Equal(t, test.expected, c.ToAlpha3(test.input), "map %s to alpha3", test.input) | ||
} | ||
} | ||
|
||
func TestCountryCodeToAlpha2(t *testing.T) { | ||
c := New() | ||
c.Load(` | ||
AD,AND | ||
AE,ARE | ||
AF,AFG | ||
`) | ||
tests := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"AND", "AD"}, | ||
{"ARE", "AE"}, | ||
{"", ""}, | ||
} | ||
|
||
for _, test := range tests { | ||
assert.Equal(t, test.expected, c.ToAlpha2(test.input), "map %s to alpha2", test.input) | ||
} | ||
} |
Oops, something went wrong.