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 (
"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 {
Code string
Names []string
DisplayName string
}
// Provinces largely follows the ISO standard: https://en.wikipedia.org/wiki/ISO_3166-2:ZA
var Provinces = []Province{
{
Code: "EC",
var Provinces = map[string]Province{
"EC": {
DisplayName: "Eastern Cape",
Names: []string{
"Eastern Cape",
"Eastern-Cape",
......@@ -33,8 +43,8 @@ var Provinces = []Province{
"Mpumalanga-Koloni",
},
},
{
Code: "FS",
"FS": {
DisplayName: "Free State",
Names: []string{
"Free State",
"Freestate",
......@@ -48,18 +58,21 @@ var Provinces = []Province{
"Freyisitata",
},
},
{
Code: "GP",
"GP": {
DisplayName: "Gauteng",
Names: []string{
"GT",
"Gauteng",
"iGauteng",
"Kgauteng",
"Rhawuti",
},
},
{
Code: "KZN",
"KZN": {
DisplayName: "KwaZulu-Natal",
Names: []string{
"NT",
"NL",
"KwaZulu-Natal",
"KwaZulu Natal",
"iKwaZulu-Natal",
......@@ -70,22 +83,23 @@ var Provinces = []Province{
"KwaZulu-Natala",
},
},
{
Code: "LP",
"LP": {
DisplayName: "Limpopo",
Names: []string{
"NP",
"Limpopo",
"Vhembe",
},
},
{
Code: "MP",
"MP": {
DisplayName: "Mpumalanga",
Names: []string{
"Mpumalanga",
"iMpumalanga",
},
},
{
Code: "NC",
"NC": {
DisplayName: "Northern Cape",
Names: []string{
"Northern Cape",
"Northern-Cape",
......@@ -102,8 +116,8 @@ var Provinces = []Province{
"Nyakatho-Koloni",
},
},
{
Code: "NW",
"NW": {
DisplayName: "North West",
Names: []string{
"North West",
"North-West",
......@@ -119,8 +133,8 @@ var Provinces = []Province{
"Nyakatho-Ntshonalanga",
},
},
{
Code: "WC",
"WC": {
DisplayName: "Western Cape",
Names: []string{
"Western Cape",
"Western-Cape",
......@@ -212,8 +226,6 @@ func stripUnwantedCharacters(s string) string {
func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string) {
newCountry = countryToClean
southAfricaVariations := []string{"ZA", "South Africa", "Suid-Afrika", "Suid Afrika", "Iningizimu Afrika", "Mzantsi Afrika", "Afrika Boroa", "Africa Kusini"}
for _, southAfricaVariation := range southAfricaVariations {
if countryToClean == nil || len(*countryToClean) == 0 || strings.ToLower(*countryToClean) == strings.ToLower(southAfricaVariation) {
defaultCountry := "South Africa"
......@@ -224,20 +236,10 @@ func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string
if *newCountry == "South Africa" && zoneToClean != nil {
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 {
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
}
func IsProvince(address string) bool {
for _, province := range Provinces {
if strings.ToLower(address) == strings.ToLower(fmt.Sprintf("%v, South Africa", province)) {
for provinceCode, province := range Provinces {
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 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) {
name: "IsProvince",
args: args{address: "North West, South Africa"},
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",
args: args{address: "22 Kruis Street, Potchefstroom, Potchefstroom, 2531, GP, ZA"},
......@@ -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