Skip to content

Commit 6ae7779

Browse files
authored
Merge pull request #68 from gobuffalo/bugfix-titlerize-unicode
fixed incorrect titleizing of unicode string
2 parents 9226f5f + 47259c7 commit 6ae7779

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ go 1.16
55
exclude github.com/stretchr/testify v1.7.1
66

77
require github.com/stretchr/testify v1.8.1
8+
9+
retract [v1.0.0, v1.0.1]

humanize_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func Test_Humanize(t *testing.T) {
2020
{"first_name", "First name"},
2121
{"first_Name", "First Name"},
2222
{"firstName", "First Name"},
23+
{"óbito", "Óbito"},
2324
}
2425

2526
for _, tt := range table {

titleize.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,20 @@ func Titleize(s string) string {
1919
// "This is `code` ok" = "This Is `code` OK"
2020
func (i Ident) Titleize() Ident {
2121
var parts []string
22+
23+
// TODO: we need to reconsider the design.
24+
// this approach preserves inline code block as is but it also
25+
// preserves the other words start with a special character.
26+
// I would prefer: "*wonderful* world" to be "*Wonderful* World"
2227
for _, part := range i.Parts {
23-
x := string(unicode.ToTitle(rune(part[0])))
24-
if len(part) > 1 {
25-
x += part[1:]
28+
// CAUTION: in unicode, []rune(str)[0] is not rune(str[0])
29+
runes := []rune(part)
30+
x := string(unicode.ToTitle(runes[0]))
31+
if len(runes) > 1 {
32+
x += string(runes[1:])
2633
}
2734
parts = append(parts, x)
2835
}
36+
2937
return New(strings.Join(parts, " "))
3038
}

titleize_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ func Test_Titleize(t *testing.T) {
1212
{"bob dylan", "Bob Dylan"},
1313
{"Nice to see you!", "Nice To See You!"},
1414
{"*hello*", "*hello*"},
15+
{"hello *wonderful* world!", "Hello *wonderful* World!"}, // CHKME
1516
{"i've read a book! have you?", "I've Read A Book! Have You?"},
1617
{"This is `code` ok", "This Is `code` OK"},
1718
{"foo_bar", "Foo Bar"},
1819
{"admin/widget", "Admin Widget"},
1920
{"widget", "Widget"},
21+
{"óbito", "Óbito"},
2022
}
2123

2224
for _, tt := range table {

0 commit comments

Comments
 (0)