From 3f4f6b5040ae55a158e02322fd1961b03479d835 Mon Sep 17 00:00:00 2001 From: sincos Date: Tue, 16 Jul 2024 01:23:08 +0800 Subject: [PATCH] fix(misc/stringsx): fix naming --- misc/stringsx/naming.go | 95 ++++++++++++++++++------------------ misc/stringsx/naming_test.go | 6 +++ 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/misc/stringsx/naming.go b/misc/stringsx/naming.go index fb2cbe3..5b889ab 100644 --- a/misc/stringsx/naming.go +++ b/misc/stringsx/naming.go @@ -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 }) } @@ -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]) } @@ -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) @@ -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 := "" @@ -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": {}, } diff --git a/misc/stringsx/naming_test.go b/misc/stringsx/naming_test.go index 3013052..fa5a039 100644 --- a/misc/stringsx/naming_test.go +++ b/misc/stringsx/naming_test.go @@ -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")) @@ -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")) }