diff --git a/address_utils/address_utils.go b/address_utils/address_utils.go index e4ef6cea68ba3fb4436f11f5fa488574c5206734..eddd3f8c70052cf6ab3a581b7bbed80ebe10f32f 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 0000000000000000000000000000000000000000..03f79db634025dc57719049152937be49ad7c9cf --- /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) + } + }) + } +}