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",