Skip to content
Snippets Groups Projects
Select Git revision
  • d88a3faf4fe49962b487f610f7990f541ce566e0
  • main default protected
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
22 results

address_utils.go

Blame
  • 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, "")