Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stringutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

Expand Down
6 changes: 6 additions & 0 deletions stringutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down