Skip to content
Snippets Groups Projects
Commit 43673d55 authored by Cornel Rautenbach's avatar Cornel Rautenbach
Browse files

Address utils

parent 8ed5cbdd
No related branches found
No related tags found
No related merge requests found
package address_utils
import (
"crypto/md5"
"fmt"
"gitlab.com/uafrica/go-utils/string_utils"
"regexp"
"strings"
)
// MD5HashOfAddress 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) string {
valueToHash := enteredAddress
if lat != nil && lng != nil {
valueToHash += fmt.Sprintf(",%v,%v", *lat, *lng)
}
return fmt.Sprintf("%X", md5.Sum([]byte(valueToHash)))
}
// MD5HashOfLowerCaseEnteredAddress 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)))
}
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, "")
return s
}
func stripUnwantedCharacters(s string) string {
re := regexp.MustCompile(`[^\s\w,-]`)
s = re.ReplaceAllString(s, "")
return s
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment