From 47259c7d220fc927a6d1e8b3fafbb06c5962ac5d Mon Sep 17 00:00:00 2001 From: Yonghwan SO Date: Fri, 24 Feb 2023 23:33:04 +0900 Subject: [PATCH] fixed incorrect titleizing of unicode string --- go.mod | 2 ++ humanize_test.go | 1 + titleize.go | 14 +++++++++++--- titleize_test.go | 2 ++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e7c6303..66bab2d 100644 --- a/go.mod +++ b/go.mod @@ -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] diff --git a/humanize_test.go b/humanize_test.go index b2a963a..2186dde 100644 --- a/humanize_test.go +++ b/humanize_test.go @@ -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 { diff --git a/titleize.go b/titleize.go index cbbf08a..0878ada 100644 --- a/titleize.go +++ b/titleize.go @@ -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, " ")) } diff --git a/titleize_test.go b/titleize_test.go index ac0975a..8e6c44a 100644 --- a/titleize_test.go +++ b/titleize_test.go @@ -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 {