From 9fc0b76dd8d29120b77ace11a63cfad44a0c9b73 Mon Sep 17 00:00:00 2001
From: Johan de Klerk <jdeklerk00@gmail.com>
Date: Wed, 9 Mar 2022 11:42:43 +0200
Subject: [PATCH] Added IsProvince to address utils

---
 address_utils/address_utils.go      | 52 ++++++++++++++++++++++++-----
 address_utils/address_utils_test.go | 32 ++++++++++++++++++
 2 files changed, 75 insertions(+), 9 deletions(-)
 create mode 100644 address_utils/address_utils_test.go

diff --git a/address_utils/address_utils.go b/address_utils/address_utils.go
index e4ef6ce..eddd3f8 100644
--- a/address_utils/address_utils.go
+++ b/address_utils/address_utils.go
@@ -8,6 +8,30 @@ import (
 	"strings"
 )
 
+const (
+	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{
+	ProvinceKwaZuluNatal,
+	ProvinceGauteng,
+	ProvinceFreeState,
+	ProvinceLimpopo,
+	ProvinceMpumalanga,
+	ProvinceNorthWest,
+	ProvinceEasternCape,
+	ProvinceWesternCape,
+	ProvinceNorthernCape,
+}
+
 // 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
 func MD5HashOfAddress(enteredAddress string, lat *float64, lng *float64, addressType *string) string {
 	valueToHash := enteredAddress
@@ -124,17 +148,17 @@ func CleanZone(oldCountry, oldZone *string) (newCountry, newZone *string) {
 			zone = "KZN"
 		}
 
-		zone = string_utils.ReplaceCaseInsensitive(zone, "KwaZulu-Natal", "KZN")
+		zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceKwaZuluNatal, "KZN")
 		zone = string_utils.ReplaceCaseInsensitive(zone, "KwaZulu Natal", "KZN")
-		zone = string_utils.ReplaceCaseInsensitive(zone, "Gauteng", "GP")
+		zone = string_utils.ReplaceCaseInsensitive(zone, ProvinceGauteng, "GP")
 		zone = string_utils.ReplaceCaseInsensitive(zone, "Freestate", "FS")
-		zone = string_utils.ReplaceCaseInsensitive(zone, "Free State", "FS")
-		zone = string_utils.ReplaceCaseInsensitive(zone, "Limpopo", "LP")
-		zone = string_utils.ReplaceCaseInsensitive(zone, "Mpumalanga", "MP")
-		zone = string_utils.ReplaceCaseInsensitive(zone, "North West", "NW")
-		zone = string_utils.ReplaceCaseInsensitive(zone, "Eastern Cape", "EC")
-		zone = string_utils.ReplaceCaseInsensitive(zone, "Western Cape", "WC")
-		zone = string_utils.ReplaceCaseInsensitive(zone, "Northern Cape", "NC")
+		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")
@@ -144,3 +168,13 @@ func CleanZone(oldCountry, oldZone *string) (newCountry, newZone *string) {
 
 	return
 }
+
+func IsProvince(address string) bool {
+	for _, province := range Provinces {
+		if strings.ToLower(address) == strings.ToLower(fmt.Sprintf("%v, South Africa", province)) {
+			return true
+		}
+	}
+
+	return false
+}
diff --git a/address_utils/address_utils_test.go b/address_utils/address_utils_test.go
new file mode 100644
index 0000000..03f79db
--- /dev/null
+++ b/address_utils/address_utils_test.go
@@ -0,0 +1,32 @@
+package address_utils
+
+import (
+	"testing"
+)
+
+func TestIsProvince(t *testing.T) {
+	type args struct {
+		address string
+	}
+	tests := []struct {
+		name string
+		args args
+		want bool
+	}{{
+		name: "IsProvince",
+		args: args{address: "North West, South Africa"},
+		want: true,
+	}, {
+		name: "IsNotProvince",
+		args: args{address: "22 Kruis Street, Potchefstroom, Potchefstroom, 2531, GP, ZA"},
+		want: false,
+	},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := IsProvince(tt.args.address); got != tt.want {
+				t.Errorf("IsProvince() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
-- 
GitLab