Skip to content

Commit

Permalink
fix(misc/stringsx): fix naming
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Jul 15, 2024
1 parent 55420ff commit 3f4f6b5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 47 deletions.
95 changes: 48 additions & 47 deletions misc/stringsx/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
// LowerSnakeCase e.g. i_am_a_10_years_senior
func LowerSnakeCase(name string) string {
return rewords(name, func(res, word string, idx int) string {
lower := strings.ToLower(word)
if idx == 0 {
return res + strings.ToLower(word)
return res + lower
}
return res + "_" + strings.ToLower(word)
return res + "_" + lower
})
}

Expand All @@ -28,8 +29,8 @@ func UpperSnakeCase(name string) string {
// LowerCamelCase e.g. iAmA10YearsSenior
func LowerCamelCase(name string) string {
return rewords(name, func(res, word string, idx int) string {
word = strings.ToLower(word)
runes := []rune(word)
lower := strings.ToLower(word)
runes := []rune(lower)
if idx > 0 {
runes[0] = unicode.ToUpper(runes[0])
}
Expand All @@ -41,7 +42,7 @@ func LowerCamelCase(name string) string {
func UpperCamelCase(name string) string {
return rewords(name, func(res, word string, idx int) string {
upper := strings.ToUpper(word)
if _, ok := initialisms[upper]; ok {
if _, ok := initialism[upper]; ok {
return res + upper
}
word = strings.ToLower(word)
Expand All @@ -62,9 +63,9 @@ func LowerDashJoint(name string) string {
})
}

type jointer func(result, word string, index int) string
type joint func(result, word string, index int) string

func rewords(s string, fn jointer) string {
func rewords(s string, fn joint) string {
words := SplitToWords(s)
ret := ""

Expand All @@ -74,44 +75,44 @@ func rewords(s string, fn jointer) string {
return ret
}

var initialisms = map[string]bool{
"ACL": true,
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SQL": true,
"SSH": true,
"TCP": true,
"TLS": true,
"TTL": true,
"UDP": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
"XMPP": true,
"XSRF": true,
"XSS": true,
"QOS": true,
var initialism = map[string]struct{}{
"ACL": {},
"API": {},
"ASCII": {},
"CPU": {},
"CSS": {},
"DNS": {},
"EOF": {},
"GUID": {},
"HTML": {},
"HTTP": {},
"HTTPS": {},
"ID": {},
"IP": {},
"JSON": {},
"LHS": {},
"QPS": {},
"RAM": {},
"RHS": {},
"RPC": {},
"SLA": {},
"SMTP": {},
"SQL": {},
"SSH": {},
"TCP": {},
"TLS": {},
"TTL": {},
"UDP": {},
"UI": {},
"UID": {},
"UUID": {},
"URI": {},
"URL": {},
"UTF8": {},
"VM": {},
"XML": {},
"XMPP": {},
"XSRF": {},
"XSS": {},
"QOS": {},
}
6 changes: 6 additions & 0 deletions misc/stringsx/naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
)

func TestNaming(t *testing.T) {
t.Run("Common", func(t *testing.T) {
})
name := "i_am_a_10_years_senior"

NewWithT(t).Expect(LowerCamelCase(name)).To(Equal("iAmA10YearsSenior"))
Expand All @@ -19,4 +21,8 @@ func TestNaming(t *testing.T) {
NewWithT(t).Expect(LowerDashJoint(name)).To(Equal("i-am-a-10-years-senior"))

NewWithT(t).Expect(UpperCamelCase("OrgID")).To(Equal("OrgID"))
NewWithT(t).Expect(LowerCamelCase("OrgID")).To(Equal("orgId"))
NewWithT(t).Expect(LowerSnakeCase("OrgID")).To(Equal("org_id"))
NewWithT(t).Expect(UpperSnakeCase("OrgID")).To(Equal("ORG_ID"))
NewWithT(t).Expect(LowerDashJoint("OrgID")).To(Equal("org-id"))
}

0 comments on commit 3f4f6b5

Please sign in to comment.