Skip to content
Snippets Groups Projects
Commit f4f9b54d authored by Francé Wilke's avatar Francé Wilke
Browse files

Add address utils function GetZoneDisplayName

parent aa796974
No related branches found
No related tags found
No related merge requests found
...@@ -9,15 +9,25 @@ import ( ...@@ -9,15 +9,25 @@ import (
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils" "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils"
) )
var southAfricaVariations = []string{
"ZA",
"South Africa",
"Suid-Afrika",
"Suid Afrika",
"Iningizimu Afrika",
"Mzantsi Afrika",
"Afrika Boroa",
"Africa Kusini",
}
type Province struct { type Province struct {
Code string
Names []string Names []string
DisplayName string
} }
// Provinces largely follows the ISO standard: https://en.wikipedia.org/wiki/ISO_3166-2:ZA var Provinces = map[string]Province{
var Provinces = []Province{ "EC": {
{ DisplayName: "Eastern Cape",
Code: "EC",
Names: []string{ Names: []string{
"Eastern Cape", "Eastern Cape",
"Eastern-Cape", "Eastern-Cape",
...@@ -33,8 +43,8 @@ var Provinces = []Province{ ...@@ -33,8 +43,8 @@ var Provinces = []Province{
"Mpumalanga-Koloni", "Mpumalanga-Koloni",
}, },
}, },
{ "FS": {
Code: "FS", DisplayName: "Free State",
Names: []string{ Names: []string{
"Free State", "Free State",
"Freestate", "Freestate",
...@@ -48,18 +58,21 @@ var Provinces = []Province{ ...@@ -48,18 +58,21 @@ var Provinces = []Province{
"Freyisitata", "Freyisitata",
}, },
}, },
{ "GP": {
Code: "GP", DisplayName: "Gauteng",
Names: []string{ Names: []string{
"GT",
"Gauteng", "Gauteng",
"iGauteng", "iGauteng",
"Kgauteng", "Kgauteng",
"Rhawuti", "Rhawuti",
}, },
}, },
{ "KZN": {
Code: "KZN", DisplayName: "KwaZulu-Natal",
Names: []string{ Names: []string{
"NT",
"NL",
"KwaZulu-Natal", "KwaZulu-Natal",
"KwaZulu Natal", "KwaZulu Natal",
"iKwaZulu-Natal", "iKwaZulu-Natal",
...@@ -70,22 +83,23 @@ var Provinces = []Province{ ...@@ -70,22 +83,23 @@ var Provinces = []Province{
"KwaZulu-Natala", "KwaZulu-Natala",
}, },
}, },
{ "LP": {
Code: "LP", DisplayName: "Limpopo",
Names: []string{ Names: []string{
"NP",
"Limpopo", "Limpopo",
"Vhembe", "Vhembe",
}, },
}, },
{ "MP": {
Code: "MP", DisplayName: "Mpumalanga",
Names: []string{ Names: []string{
"Mpumalanga", "Mpumalanga",
"iMpumalanga", "iMpumalanga",
}, },
}, },
{ "NC": {
Code: "NC", DisplayName: "Northern Cape",
Names: []string{ Names: []string{
"Northern Cape", "Northern Cape",
"Northern-Cape", "Northern-Cape",
...@@ -102,8 +116,8 @@ var Provinces = []Province{ ...@@ -102,8 +116,8 @@ var Provinces = []Province{
"Nyakatho-Koloni", "Nyakatho-Koloni",
}, },
}, },
{ "NW": {
Code: "NW", DisplayName: "North West",
Names: []string{ Names: []string{
"North West", "North West",
"North-West", "North-West",
...@@ -119,8 +133,8 @@ var Provinces = []Province{ ...@@ -119,8 +133,8 @@ var Provinces = []Province{
"Nyakatho-Ntshonalanga", "Nyakatho-Ntshonalanga",
}, },
}, },
{ "WC": {
Code: "WC", DisplayName: "Western Cape",
Names: []string{ Names: []string{
"Western Cape", "Western Cape",
"Western-Cape", "Western-Cape",
...@@ -212,8 +226,6 @@ func stripUnwantedCharacters(s string) string { ...@@ -212,8 +226,6 @@ func stripUnwantedCharacters(s string) string {
func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string) { func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string) {
newCountry = countryToClean newCountry = countryToClean
southAfricaVariations := []string{"ZA", "South Africa", "Suid-Afrika", "Suid Afrika", "Iningizimu Afrika", "Mzantsi Afrika", "Afrika Boroa", "Africa Kusini"}
for _, southAfricaVariation := range southAfricaVariations { for _, southAfricaVariation := range southAfricaVariations {
if countryToClean == nil || len(*countryToClean) == 0 || strings.ToLower(*countryToClean) == strings.ToLower(southAfricaVariation) { if countryToClean == nil || len(*countryToClean) == 0 || strings.ToLower(*countryToClean) == strings.ToLower(southAfricaVariation) {
defaultCountry := "South Africa" defaultCountry := "South Africa"
...@@ -224,20 +236,10 @@ func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string ...@@ -224,20 +236,10 @@ func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string
if *newCountry == "South Africa" && zoneToClean != nil { if *newCountry == "South Africa" && zoneToClean != nil {
zone := *zoneToClean zone := *zoneToClean
if zone == "GT" {
// Gauteng - GT should be GP for Google
zone = "GP"
} else if zone == "NT" || zone == "NL" {
// KZN - NT and NL should be KZN for Google
zone = "KZN"
} else if zone == "NP" {
// Limpopo - NP should be LP for Google
zone = "LP"
}
for _, province := range Provinces { for provinceCode, province := range Provinces {
for _, name := range province.Names { for _, name := range province.Names {
zone = string_utils.ReplaceCaseInsensitive(zone, name, province.Code) zone = string_utils.ReplaceCaseInsensitive(zone, name, provinceCode)
} }
} }
...@@ -248,11 +250,28 @@ func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string ...@@ -248,11 +250,28 @@ func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string
} }
func IsProvince(address string) bool { func IsProvince(address string) bool {
for _, province := range Provinces { for provinceCode, province := range Provinces {
if strings.ToLower(address) == strings.ToLower(fmt.Sprintf("%v, South Africa", province)) { if strings.ToLower(address) == strings.ToLower(fmt.Sprintf("%v, South Africa", provinceCode)) {
return true
}
if strings.ToLower(address) == strings.ToLower(fmt.Sprintf("%v, South Africa", province.DisplayName)) {
return true
}
for _, provinceName := range province.Names {
if strings.ToLower(address) == strings.ToLower(fmt.Sprintf("%v, South Africa", provinceName)) {
return true return true
} }
} }
}
return false return false
} }
func GetZoneDisplayName(zone string) string {
if province, ok := Provinces[zone]; ok {
return province.DisplayName
}
return zone
}
...@@ -25,6 +25,14 @@ func TestIsProvince(t *testing.T) { ...@@ -25,6 +25,14 @@ func TestIsProvince(t *testing.T) {
name: "IsProvince", name: "IsProvince",
args: args{address: "North West, South Africa"}, args: args{address: "North West, South Africa"},
want: true, want: true,
}, {
name: "IsProvince2",
args: args{address: "KwaZulu Natal, South Africa"},
want: true,
}, {
name: "IsProvince3",
args: args{address: "KZN, South Africa"},
want: true,
}, { }, {
name: "IsNotProvince", name: "IsNotProvince",
args: args{address: "22 Kruis Street, Potchefstroom, Potchefstroom, 2531, GP, ZA"}, args: args{address: "22 Kruis Street, Potchefstroom, Potchefstroom, 2531, GP, ZA"},
...@@ -39,3 +47,38 @@ func TestIsProvince(t *testing.T) { ...@@ -39,3 +47,38 @@ func TestIsProvince(t *testing.T) {
}) })
} }
} }
func TestZoneDisplayName(t *testing.T) {
type args struct {
zone string
}
tests := []struct {
name string
args args
want string
}{{
name: "IsValidZone",
args: args{zone: "LP"},
want: "Limpopo",
}, {
name: "IsValidZone2",
args: args{zone: "KZN"},
want: "KwaZulu-Natal",
}, {
name: "IsNotValidZone",
args: args{zone: "invalidF"},
want: "invalidF",
}, {
name: "IsNotValidZone 2",
args: args{zone: "NP"},
want: "NP",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetZoneDisplayName(tt.args.zone); got != tt.want {
t.Errorf("GetZoneDisplayName() = %v, want %v", got, tt.want)
}
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment