Select Git revision
address_utils.go
address_utils.go 3.96 KiB
package address_utils
import (
"crypto/md5"
"fmt"
"gitlab.com/uafrica/go-utils/string_utils"
"regexp"
"strings"
)
// 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
if lat != nil && lng != nil {
valueToHash += fmt.Sprintf(",%v,%v", *lat, *lng)
}
if addressType != nil && len(*addressType) > 0 && *addressType != "unknown" {
valueToHash += fmt.Sprintf(",%s", *addressType)
}
return fmt.Sprintf("%X", md5.Sum([]byte(valueToHash)))
}
// MD5HashOfLowerCaseEnteredAddress m(e) - calculates and returns the MD5 hash of a string after preparing the string properly.
func MD5HashOfLowerCaseEnteredAddress(enteredAddress string) string {
valueToHash := cleanLowerCaseAddress(enteredAddress)
return fmt.Sprintf("%X", md5.Sum([]byte(valueToHash)))
}
// cleanLowerCaseAddress makes the entered address lowercase, removes unwanted chars, strip street terms and removes all whitespace
func cleanLowerCaseAddress(enteredAddress string) string {
// Lowercase.
enteredAddress = strings.ToLower(enteredAddress)
// Remove unwanted characters.
enteredAddress = stripUnwantedCharacters(enteredAddress)
// Remove ave, str, etc.
enteredAddress = stripStreetTerms(enteredAddress)
// Remove all whitespace.
enteredAddress = string_utils.RemoveAllWhiteSpaces(enteredAddress)
return enteredAddress
}
func stripStreetTerms(s string) string {
terms := []string{
"ave",
"avenue",
"str",
"street",
"st",
"straat",
"lane",
"ln",
"laan",
"road",
"rd",
"boulevard",
"blvd",
"drive",
"drv",
"way",
"weg",
}
re := regexp.MustCompile(`\s+(` + strings.Join(terms, "|") + `)[\s]*`)
s = re.ReplaceAllString(s, "")