From 96e7066c2ede1a396e3e8650fb4feae75b5592b3 Mon Sep 17 00:00:00 2001 From: Daniel Bae <157205701+MrBeldum@users.noreply.github.com> Date: Mon, 5 Oct 2026 14:01:59 -0700 Subject: [PATCH] fix: return empty string for non-positive TruncateWords maxWords --- stringutils.go | 4 ++-- stringutils_test.go | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/stringutils.go b/stringutils.go index b4aa4cd..7cd7313 100644 --- a/stringutils.go +++ b/stringutils.go @@ -128,9 +128,9 @@ func Truncate(s string, maxLen int) string { } // TruncateWords cuts string to the given number of words and adds ellipsis if it was truncated -// returns empty string if maxWords is 0 +// returns empty string if maxWords is 0 or negative func TruncateWords(s string, maxWords int) string { - if maxWords == 0 { + if maxWords <= 0 { return "" } diff --git a/stringutils_test.go b/stringutils_test.go index 530d224..2cf5866 100644 --- a/stringutils_test.go +++ b/stringutils_test.go @@ -391,6 +391,12 @@ func TestTruncateWords(t *testing.T) { maxWords: 0, want: "", }, + { + name: "negative max words", + input: "hello beautiful world", + maxWords: -1, + want: "", + }, { name: "multiple spaces between words", input: "hello beautiful world",