Skip to content

Commit

Permalink
chore: untitle first word of a string (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
loicbourgois authored Feb 26, 2020
1 parent 5d2ba47 commit 7bdc780
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
20 changes: 19 additions & 1 deletion strcase/goname.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ var customInitialisms = map[string][2]string{

// TitleFirstWord upper case the first letter of a string.
func TitleFirstWord(s string) string {
if len(s) == 0 {
if s == "" {
return s
}

Expand All @@ -168,6 +168,24 @@ func TitleFirstWord(s string) string {
return string(r)
}

// UntitleFirstWord lower case the first letter of a string.
func UntitleFirstWord(s string) string {
if s == "" {
return s
}

r := []rune(s)

firstWord := strings.Split(s, " ")[0]
_, isCommonInitialism := commonInitialisms[firstWord]
_, isCustomInitialism := customInitialisms[firstWord]
if !isCommonInitialism && !isCustomInitialism {
r[0] = unicode.ToLower(r[0])
}

return string(r)
}

// lowerCaseFirstLetterOrAcronyms lower case the first letter of a string.
func lowerCaseFirstLetterOrAcronyms(s string) string {
r := []rune(s)
Expand Down
18 changes: 18 additions & 0 deletions strcase/goname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@ func TestTitleFirstWord(t *testing.T) {
}
}
}

func Test_UntitleFirstWord(t *testing.T) {
cases := [][]string{
{"", ""},
{"T", "t"},
{"UUID", "UUID"},
{"UUI", "uUI"},
{"TEST CASE", "tEST CASE"},
}
for _, i := range cases {
in := i[0]
out := i[1]
result := UntitleFirstWord(in)
if result != out {
t.Error("'" + result + "' != '" + out + "'")
}
}
}

0 comments on commit 7bdc780

Please sign in to comment.