Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
43673d55
Commit
43673d55
authored
3 years ago
by
Cornel Rautenbach
Browse files
Options
Downloads
Patches
Plain Diff
Address utils
parent
8ed5cbdd
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
address_utils/address_utils.go
+74
-0
74 additions, 0 deletions
address_utils/address_utils.go
with
74 additions
and
0 deletions
address_utils/address_utils.go
0 → 100644
+
74
−
0
View file @
43673d55
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
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment