Skip to content

Commit

Permalink
Add strict option to ToTitle
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Feb 26, 2024
1 parent 5ff7dd7 commit 0901558
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions internal/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ func CharAt(s string, i int) byte {

// ToTitle returns a copy of the string m with its first Unicode letter mapped
// to its title case.
func ToTitle(m string) string {
func ToTitle(m string, strict bool) string {
r, size := utf8.DecodeRuneInString(m)
return string(unicode.ToTitle(r)) + strings.ToLower(m[size:])

other := m[size:]
if strict {
other = strings.ToLower(other)
}

return string(unicode.ToTitle(r)) + other
}
2 changes: 1 addition & 1 deletion strcase/sentence.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (sc *SentenceConverter) Convert(s string) string {
if entry := sc.inVocab(token); entry != "" {
made = append(made, entry)
} else if i == 0 || sc.indicator(prev, i-1) {
made = append(made, internal.ToTitle(token))
made = append(made, internal.ToTitle(token, true))
} else {
made = append(made, strings.ToLower(token))
}
Expand Down
3 changes: 2 additions & 1 deletion strcase/title.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func (tc *TitleConverter) Convert(s string) string {
(internal.CharAt(t, pos+ext) != '-' || internal.CharAt(t, pos-1) == '-') {
return sm
}
return internal.ToTitle(m)

return internal.ToTitle(m, false)
})
}

Expand Down

0 comments on commit 0901558

Please sign in to comment.