Skip to content

Commit

Permalink
Merge pull request #68 from gobuffalo/bugfix-titlerize-unicode
Browse files Browse the repository at this point in the history
fixed incorrect titleizing of unicode string
  • Loading branch information
sio4 committed Feb 24, 2023
2 parents 9226f5f + 47259c7 commit 6ae7779
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ go 1.16
exclude github.com/stretchr/testify v1.7.1

require github.com/stretchr/testify v1.8.1

retract [v1.0.0, v1.0.1]
1 change: 1 addition & 0 deletions humanize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Test_Humanize(t *testing.T) {
{"first_name", "First name"},
{"first_Name", "First Name"},
{"firstName", "First Name"},
{"óbito", "Óbito"},
}

for _, tt := range table {
Expand Down
14 changes: 11 additions & 3 deletions titleize.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ func Titleize(s string) string {
// "This is `code` ok" = "This Is `code` OK"
func (i Ident) Titleize() Ident {
var parts []string

// TODO: we need to reconsider the design.
// this approach preserves inline code block as is but it also
// preserves the other words start with a special character.
// I would prefer: "*wonderful* world" to be "*Wonderful* World"
for _, part := range i.Parts {
x := string(unicode.ToTitle(rune(part[0])))
if len(part) > 1 {
x += part[1:]
// CAUTION: in unicode, []rune(str)[0] is not rune(str[0])
runes := []rune(part)
x := string(unicode.ToTitle(runes[0]))
if len(runes) > 1 {
x += string(runes[1:])
}
parts = append(parts, x)
}

return New(strings.Join(parts, " "))
}
2 changes: 2 additions & 0 deletions titleize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ func Test_Titleize(t *testing.T) {
{"bob dylan", "Bob Dylan"},
{"Nice to see you!", "Nice To See You!"},
{"*hello*", "*hello*"},
{"hello *wonderful* world!", "Hello *wonderful* World!"}, // CHKME
{"i've read a book! have you?", "I've Read A Book! Have You?"},
{"This is `code` ok", "This Is `code` OK"},
{"foo_bar", "Foo Bar"},
{"admin/widget", "Admin Widget"},
{"widget", "Widget"},
{"óbito", "Óbito"},
}

for _, tt := range table {
Expand Down

0 comments on commit 6ae7779

Please sign in to comment.