Skip to content

Commit 7bdc780

Browse files
authored
chore: untitle first word of a string (scaleway#338)
1 parent 5d2ba47 commit 7bdc780

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

strcase/goname.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ var customInitialisms = map[string][2]string{
158158

159159
// TitleFirstWord upper case the first letter of a string.
160160
func TitleFirstWord(s string) string {
161-
if len(s) == 0 {
161+
if s == "" {
162162
return s
163163
}
164164

@@ -168,6 +168,24 @@ func TitleFirstWord(s string) string {
168168
return string(r)
169169
}
170170

171+
// UntitleFirstWord lower case the first letter of a string.
172+
func UntitleFirstWord(s string) string {
173+
if s == "" {
174+
return s
175+
}
176+
177+
r := []rune(s)
178+
179+
firstWord := strings.Split(s, " ")[0]
180+
_, isCommonInitialism := commonInitialisms[firstWord]
181+
_, isCustomInitialism := customInitialisms[firstWord]
182+
if !isCommonInitialism && !isCustomInitialism {
183+
r[0] = unicode.ToLower(r[0])
184+
}
185+
186+
return string(r)
187+
}
188+
171189
// lowerCaseFirstLetterOrAcronyms lower case the first letter of a string.
172190
func lowerCaseFirstLetterOrAcronyms(s string) string {
173191
r := []rune(s)

strcase/goname_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ func TestTitleFirstWord(t *testing.T) {
4646
}
4747
}
4848
}
49+
50+
func Test_UntitleFirstWord(t *testing.T) {
51+
cases := [][]string{
52+
{"", ""},
53+
{"T", "t"},
54+
{"UUID", "UUID"},
55+
{"UUI", "uUI"},
56+
{"TEST CASE", "tEST CASE"},
57+
}
58+
for _, i := range cases {
59+
in := i[0]
60+
out := i[1]
61+
result := UntitleFirstWord(in)
62+
if result != out {
63+
t.Error("'" + result + "' != '" + out + "'")
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)