Skip to content

Commit

Permalink
Ensure ToTitle only capitalizes the first char
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Feb 26, 2024
1 parent 91c6aa3 commit 5ff7dd7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ func CharAt(s string, i int) byte {
// to its title case.
func ToTitle(m string) string {
r, size := utf8.DecodeRuneInString(m)
return string(unicode.ToTitle(r)) + m[size:]
return string(unicode.ToTitle(r)) + strings.ToLower(m[size:])
}
6 changes: 4 additions & 2 deletions strcase/sentence.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ func NewSentenceConverter(opts ...CaseOptFunc) *SentenceConverter {
func (sc *SentenceConverter) Convert(s string) string {
var made []string

s = strings.ToLower(s)

ps := `[\p{N}\p{L}*]+[^\s]*`
if len(sc.vocab) > 0 {
ps = fmt.Sprintf(`\b(?:%s)\b|%s`, strings.Join(sc.vocab, "|"), ps)
}
re := regexp2.MustCompileStd(`(?i)` + ps)

tokens := re.FindAllString(s, -1)
// NOTE: We have to do this *after* tokenizing the string in order to
// respect the case of would-be exceptions.
s = strings.ToLower(s)

for i, token := range tokens {
prev := ""
if i-1 >= 0 {
Expand Down
2 changes: 2 additions & 0 deletions strcase/sentence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var vocabCases = []testCase{
{"Getting started with vale server", "Getting started with Vale Server"},
{"Issue triage", "Issue triage"},
{"macOS 15: What's new", "macOS 15: What's new"},
{"Configuration", "Configuration"},
}

func TestSentence(t *testing.T) {
Expand All @@ -36,6 +37,7 @@ func TestVocab(t *testing.T) {
"Vale Server",
"I",
"macOS",
"[Cc]onfig",
}))

for _, test := range vocabCases {
Expand Down

0 comments on commit 5ff7dd7

Please sign in to comment.