From 784c899176789da1932d0fcfe75d626b47434421 Mon Sep 17 00:00:00 2001 From: Khayyam Saleem Date: Fri, 20 Sep 2024 20:41:12 -0400 Subject: [PATCH] fix for panics when humanize called on whitespace --- humanize.go | 5 +++++ humanize_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/humanize.go b/humanize.go index 311c8be..5100bfb 100644 --- a/humanize.go +++ b/humanize.go @@ -7,6 +7,7 @@ import ( // Humanize returns first letter of sentence capitalized. // Common acronyms are capitalized as well. // Other capital letters in string are left as provided. +// // employee_salary = Employee salary // employee_id = employee ID // employee_mobile_number = Employee mobile number @@ -22,6 +23,10 @@ func (i Ident) Humanize() Ident { return New("") } + if strings.TrimSpace(i.Original) == "" { + return i + } + parts := xappend([]string{}, Titleize(i.Parts[0])) if len(i.Parts) > 1 { parts = xappend(parts, i.Parts[1:]...) diff --git a/humanize_test.go b/humanize_test.go index 2186dde..2c97a61 100644 --- a/humanize_test.go +++ b/humanize_test.go @@ -21,6 +21,11 @@ func Test_Humanize(t *testing.T) { {"first_Name", "First Name"}, {"firstName", "First Name"}, {"óbito", "Óbito"}, + {" ", " "}, + {"\n", "\n"}, + {"\r", "\r"}, + {"\t", "\t"}, + {" \n\r\t", " \n\r\t"}, } for _, tt := range table {