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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
9f3f4ed9
Commit
9f3f4ed9
authored
1 year ago
by
James Page
Browse files
Options
Downloads
Patches
Plain Diff
Add HumanReadableToKey function
parent
f5320f2e
Branches
Branches containing commit
Tags
v1.148.0
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
string_utils/string_utils.go
+7
-0
7 additions, 0 deletions
string_utils/string_utils.go
string_utils/string_utils_test.go
+25
-14
25 additions, 14 deletions
string_utils/string_utils_test.go
with
32 additions
and
14 deletions
string_utils/string_utils.go
+
7
−
0
View file @
9f3f4ed9
...
...
@@ -191,6 +191,13 @@ func KeyToHumanReadable(s string) string {
return
SentenceCase
(
string
(
s
))
}
func
HumanReadableToKey
(
s
string
,
separator
string
)
string
{
re
:=
regexp
.
MustCompile
(
" +"
)
s
=
re
.
ReplaceAllString
(
s
,
separator
)
s
=
strings
.
ToLower
(
s
)
return
s
}
func
SentenceCase
(
str
string
)
string
{
if
len
(
str
)
>
0
{
str
=
strings
.
ToLower
(
str
)
...
...
This diff is collapsed.
Click to expand it.
string_utils/string_utils_test.go
+
25
−
14
View file @
9f3f4ed9
...
...
@@ -2,35 +2,46 @@ package string_utils
import
"testing"
func
Test
IsValidEmail
(
t
*
testing
.
T
)
{
func
Test
HumanReadableToKey
(
t
*
testing
.
T
)
{
type
args
struct
{
email
string
humanReadableString
string
separator
string
}
tests
:=
[]
struct
{
name
string
args
args
want
bool
want
string
}{
{
name
:
"
valid
"
,
args
:
args
{
email
:
"johan@bob.co.za
"
},
want
:
true
,
name
:
"
leading capitals with underscore
"
,
args
:
args
{
"Test Courier Name"
,
"_
"
},
want
:
"test_courier_name"
,
},
{
name
:
"
invalid
"
,
args
:
args
{
email
:
"johan@bob.co.za
"
},
want
:
false
,
name
:
"
leading capitals with hyphen
"
,
args
:
args
{
"Test Courier Name"
,
"-
"
},
want
:
"test-courier-name"
,
},
{
name
:
"invalid"
,
args
:
args
{
email
:
"johan@bob.co.za"
},
want
:
false
,
name
:
"all lowercase with underscore"
,
args
:
args
{
"test courier name"
,
"_"
},
want
:
"test_courier_name"
,
},
{
name
:
"random capitalisation with hyphen"
,
args
:
args
{
"tEsT CoURIer NAME"
,
"-"
},
want
:
"test-courier-name"
,
},
{
name
:
"weird characters"
,
args
:
args
{
"Tes*t $#@! (Couriers)"
,
"_"
},
want
:
"tes*t_$#@!_(couriers)"
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
if
got
:=
IsValidEmail
(
tt
.
args
.
email
);
got
!=
tt
.
want
{
t
.
Errorf
(
"
IsValidEmail
() = %v, want %v"
,
got
,
tt
.
want
)
if
got
:=
HumanReadableToKey
(
tt
.
args
.
humanReadableString
,
tt
.
args
.
separator
);
got
!=
tt
.
want
{
t
.
Errorf
(
"
HumanReadableToKey
() = %v, want %v"
,
got
,
tt
.
want
)
}
})
}
...
...
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