github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/x/stringsx/naming.go (about) 1 package stringsx 2 3 import ( 4 "strings" 5 "unicode" 6 ) 7 8 // LowerSnakeCase e.g. i_am_a_10_years_senior 9 func LowerSnakeCase(name string) string { 10 return rewords(name, func(res, word string, idx int) string { 11 if idx == 0 { 12 return res + strings.ToLower(word) 13 } 14 return res + "_" + strings.ToLower(word) 15 }) 16 } 17 18 // UpperSnakeCase e.g. I_AM_A_10_YEARS_SENIOR 19 func UpperSnakeCase(name string) string { 20 return rewords(name, func(res, word string, idx int) string { 21 if idx == 0 { 22 return res + strings.ToUpper(word) 23 } 24 return res + "_" + strings.ToUpper(word) 25 }) 26 } 27 28 // LowerCamelCase e.g. iAmA10YearsSenior 29 func LowerCamelCase(name string) string { 30 return rewords(name, func(res, word string, idx int) string { 31 word = strings.ToLower(word) 32 runes := []rune(word) 33 if idx > 0 { 34 runes[0] = unicode.ToUpper(runes[0]) 35 } 36 return res + string(runes) 37 }) 38 } 39 40 // UpperCamelCase e.g. IAmA10YearsSenior 41 func UpperCamelCase(name string) string { 42 return rewords(name, func(res, word string, idx int) string { 43 upper := strings.ToUpper(word) 44 if _, ok := initialisms[upper]; ok { 45 return res + upper 46 } 47 word = strings.ToLower(word) 48 runes := []rune(word) 49 runes[0] = unicode.ToUpper(runes[0]) 50 return res + string(runes) 51 }) 52 } 53 54 type jointer func(result, word string, index int) string 55 56 func rewords(s string, fn jointer) string { 57 words := SplitToWords(s) 58 ret := "" 59 60 for i, word := range words { 61 ret = fn(ret, word, i) 62 } 63 return ret 64 } 65 66 var initialisms = map[string]bool{ 67 "ACL": true, 68 "API": true, 69 "ASCII": true, 70 "CPU": true, 71 "CSS": true, 72 "DNS": true, 73 "EOF": true, 74 "GUID": true, 75 "HTML": true, 76 "HTTP": true, 77 "HTTPS": true, 78 "ID": true, 79 "IP": true, 80 "JSON": true, 81 "LHS": true, 82 "QPS": true, 83 "RAM": true, 84 "RHS": true, 85 "RPC": true, 86 "SLA": true, 87 "SMTP": true, 88 "SQL": true, 89 "SSH": true, 90 "TCP": true, 91 "TLS": true, 92 "TTL": true, 93 "UDP": true, 94 "UI": true, 95 "UID": true, 96 "UUID": true, 97 "URI": true, 98 "URL": true, 99 "UTF8": true, 100 "VM": true, 101 "XML": true, 102 "XMPP": true, 103 "XSRF": true, 104 "XSS": true, 105 "QOS": true, 106 }