Skip to content

Commit 0901558

Browse files
committed
Add strict option to ToTitle
1 parent 5ff7dd7 commit 0901558

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

internal/string.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ func CharAt(s string, i int) byte {
5959

6060
// ToTitle returns a copy of the string m with its first Unicode letter mapped
6161
// to its title case.
62-
func ToTitle(m string) string {
62+
func ToTitle(m string, strict bool) string {
6363
r, size := utf8.DecodeRuneInString(m)
64-
return string(unicode.ToTitle(r)) + strings.ToLower(m[size:])
64+
65+
other := m[size:]
66+
if strict {
67+
other = strings.ToLower(other)
68+
}
69+
70+
return string(unicode.ToTitle(r)) + other
6571
}

strcase/sentence.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (sc *SentenceConverter) Convert(s string) string {
6565
if entry := sc.inVocab(token); entry != "" {
6666
made = append(made, entry)
6767
} else if i == 0 || sc.indicator(prev, i-1) {
68-
made = append(made, internal.ToTitle(token))
68+
made = append(made, internal.ToTitle(token, true))
6969
} else {
7070
made = append(made, strings.ToLower(token))
7171
}

strcase/title.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ func (tc *TitleConverter) Convert(s string) string {
9797
(internal.CharAt(t, pos+ext) != '-' || internal.CharAt(t, pos-1) == '-') {
9898
return sm
9999
}
100-
return internal.ToTitle(m)
100+
101+
return internal.ToTitle(m, false)
101102
})
102103
}
103104

0 commit comments

Comments
 (0)