Skip to content
Snippets Groups Projects
Commit c15471e1 authored by Cornel Rautenbach's avatar Cornel Rautenbach
Browse files
parent ea28c3d4
No related branches found
No related tags found
No related merge requests found
...@@ -3,33 +3,140 @@ package address_utils ...@@ -3,33 +3,140 @@ package address_utils
import ( import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"gitlab.com/uafrica/go-utils/string_utils"
"regexp" "regexp"
"strings" "strings"
)
const ( "gitlab.com/uafrica/go-utils/string_utils"
ProvinceKwaZuluNatal string = "KwaZulu-Natal"
ProvinceGauteng string = "Gauteng"
ProvinceFreeState string = "Free State"
ProvinceLimpopo string = "Limpopo"
ProvinceMpumalanga string = "Mpumalanga"
ProvinceNorthWest string = "North West"
ProvinceEasternCape string = "Eastern Cape"
ProvinceWesternCape string = "Western Cape"
ProvinceNorthernCape string = "Northern Cape"
) )
var Provinces = []string{ type Province struct {
ProvinceKwaZuluNatal, Code string
ProvinceGauteng, Names []string
ProvinceFreeState, }
ProvinceLimpopo,
ProvinceMpumalanga, // Provinces largely follows the ISO standard: https://en.wikipedia.org/wiki/ISO_3166-2:ZA
ProvinceNorthWest, var Provinces = []Province{
ProvinceEasternCape, {
ProvinceWesternCape, Code: "EC",
ProvinceNorthernCape, Names: []string{
"Eastern Cape",
"Eastern-Cape",
"Oos-Kaap",
"iPumalanga-Kapa",
"Kapa Bohlabela",
"Kapa Botjhabela",
"Kapa-Vuxa",
"Kapa Botlhaba",
"Kapa Vhubvaḓuvha",
"Mpuma-Koloni",
"Mpumalanga-Kapa",
"Mpumalanga-Koloni",
},
},
{
Code: "FS",
Names: []string{
"Free State",
"Freestate",
"Vrystaat",
"iFreyistata",
"Freistata",
"Foreisetata",
"Fureisitata",
"Freyistata",
"Fuleyisitata",
"Freyisitata",
},
},
{
Code: "GP",
Names: []string{
"Gauteng",
"iGauteng",
"Kgauteng",
"Rhawuti",
},
},
{
Code: "KZN",
Names: []string{
"KwaZulu-Natal",
"KwaZulu Natal",
"iKwaZulu-Natal",
"GaZulu-Natala",
"Hazolo-Natala",
"KwaZulu-Natali",
"HaZulu-Natal",
"KwaZulu-Natala",
},
},
{
Code: "LP",
Names: []string{
"Limpopo",
"Vhembe",
},
},
{
Code: "MP",
Names: []string{
"Mpumalanga",
"iMpumalanga",
},
},
{
Code: "NC",
Names: []string{
"Northern Cape",
"Northern-Cape",
"Noord-Kaap",
"Noord Kaap",
"iTlhagwini-Kapa",
"Kapa Leboya",
"Kapa-N'walungu",
"Kapa Bokone",
"Kapa Leboa",
"Kapa Devhula",
"Mntla-Koloni",
"Nyakatho-Kapa",
"Nyakatho-Koloni",
},
},
{
Code: "NW",
Names: []string{
"North West",
"North-West",
"Noordwes",
"Noord-wes",
"iTlhagwini-Tjhingalanga",
"Lebowa Bodikela",
"Leboya Bophirima",
"Leboya le Bophirima",
"N'walungu-Vupeladyambu",
"Bokone Bophirima",
"Mntla-Ntshona",
"Nyakatho-Ntshonalanga",
},
},
{
Code: "WC",
Names: []string{
"Western Cape",
"Western-Cape",
"Wes-Kaap",
"Wes Kaap",
"iTjhingalanga-Kapa",
"Kapa Bodikela",
"Kapa Bophirimela",
"Kapa-Vupeladyambu",
"Kapa Bophirima",
"Kapa Vhukovhela",
"Ntshona-Koloni",
"Ntshonalanga-Kapa",
"Ntshonalanga-Koloni",
},
},
} }
// MD5HashOfAddress m(E,L,L) - calculates and returns the MD5 hash of the entered address, lat and lng concatenated together. If lat and lng is blank, it is only the hash of the entered address // MD5HashOfAddress m(E,L,L) - calculates and returns the MD5 hash of the entered address, lat and lng concatenated together. If lat and lng is blank, it is only the hash of the entered address
...@@ -102,45 +209,21 @@ func stripUnwantedCharacters(s string) string { ...@@ -102,45 +209,21 @@ func stripUnwantedCharacters(s string) string {
return s return s
} }
func CleanZone(oldCountry, oldZone *string) (newCountry, newZone *string) { func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string) {
// Google zones newCountry = countryToClean
/*
"long_name": "KwaZulu-Natal",
"short_name": "KZN",
"long_name": "Gauteng",
"short_name": "GP",
"long_name": "Free State",
"short_name": "FS",
"long_name": "Limpopo",
"short_name": "LP",
"long_name": "Mpumalanga",
"short_name": "MP",
"long_name": "North West", southAfricaVariations := []string{"South Africa", "Suid-Afrika", "Suid Afrika", "Iningizimu Afrika", "Mzantsi Afrika", "Afrika Boroa", "Africa Kusini"}
"short_name": "NW",
"long_name": "Western Cape", for _, southAfricaVariation := range southAfricaVariations {
"short_name": "WC", if countryToClean == nil || len(*countryToClean) == 0 || strings.ToLower(*countryToClean) == strings.ToLower(southAfricaVariation) {
"long_name": "Eastern Cape",
"short_name": "EC",
"long_name": "Northern Cape",
"short_name": "NC",
*/
newCountry = oldCountry
if oldCountry == nil || *oldCountry == "South Africa" || len(*oldCountry) == 0 {
defaultCountry := "ZA" defaultCountry := "ZA"
newCountry = &defaultCountry newCountry = &defaultCountry
break
}
} }
if *newCountry == "ZA" && oldZone != nil { if *newCountry == "ZA" && zoneToClean != nil {
zone := *oldZone zone := *zoneToClean
// Gauteng - GT from uAfrica should be GP for Google // Gauteng - GT from uAfrica should be GP for Google
if zone == "GT" { if zone == "GT" {
zone = "GP" zone = "GP"
...@@ -148,20 +231,11 @@ func CleanZone(oldCountry, oldZone *string) (newCountry, newZone *string) { ...@@ -148,20 +231,11 @@ func CleanZone(oldCountry, oldZone *string) (newCountry, newZone *string) {
zone = "KZN" zone = "KZN"
} }
zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceKwaZuluNatal, "KZN") for _, province := range Provinces {
zone = string_utils.ReplaceCaseInsensitive(zone, "KwaZulu Natal", "KZN") for _, name := range province.Names {
zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceGauteng, "GP") zone = string_utils.ReplaceCaseInsensitive(zone, name, province.Code)
zone = string_utils.ReplaceCaseInsensitive(zone, "Freestate", "FS") }
zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceFreeState, "FS") }
zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceLimpopo, "LP")
zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceMpumalanga, "MP")
zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceNorthWest, "NW")
zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceEasternCape, "EC")
zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceWesternCape, "WC")
zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceNorthernCape, "NC")
zone = string_utils.ReplaceCaseInsensitive(zone, "Eastern-Cape", "EC")
zone = string_utils.ReplaceCaseInsensitive(zone, "Western-Cape", "WC")
zone = string_utils.ReplaceCaseInsensitive(zone, "Northern-Cape", "NC")
newZone = &zone newZone = &zone
} }
......
package address_utils package address_utils
import ( import (
"fmt"
"testing" "testing"
) )
func TestCleanProvince(t *testing.T) {
zone := "Wes KaaP"
country := "South Africa"
cleanCountry, cleanZone := CleanZone(&country, &zone)
fmt.Printf("%s, %s converted to %s, %s\n", zone, country, *cleanZone, *cleanCountry)
}
func TestIsProvince(t *testing.T) { func TestIsProvince(t *testing.T) {
type args struct { type args struct {
address string address string
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment