From 644fe82053190e163a9edcdc548e558606e68e2c Mon Sep 17 00:00:00 2001 From: Nick Chen Date: Tue, 18 Jun 2024 15:26:45 -0700 Subject: [PATCH] fix: no panic after on defaultNamifier --- pkg/utils/template/helpers.go | 3 +++ pkg/utils/template/helpers_test.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/pkg/utils/template/helpers.go b/pkg/utils/template/helpers.go index 0d5c21cf..83dc8a8c 100644 --- a/pkg/utils/template/helpers.go +++ b/pkg/utils/template/helpers.go @@ -61,6 +61,9 @@ func DefaultNamifier(sentence string) string { // Remove leading numbers re = regexp.MustCompile("^[0-9]+") sentence = string(re.ReplaceAll([]byte(sentence), []byte(""))) + if len(sentence) == 0 { + return sentence + } // Upper first letter sentence = strings.ToUpper(sentence[:1]) + sentence[1:] diff --git a/pkg/utils/template/helpers_test.go b/pkg/utils/template/helpers_test.go index 93aef486..85d7d9ae 100644 --- a/pkg/utils/template/helpers_test.go +++ b/pkg/utils/template/helpers_test.go @@ -22,6 +22,10 @@ type namifyCases struct { var namifyBaseCases = []namifyCases{ // Nothing {In: "", Out: ""}, + // new line + {In: "\n", Out: ""}, + // just numbers + {In: "00", Out: ""}, // Remove leading digits {In: "0name0", Out: "Name0"}, // Remove non alphanumerics