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
3 changes: 2 additions & 1 deletion slugify/slugify.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def smart_truncate(

string = string.strip(separator)

if not max_length:
# Non-positive max_length means no truncation (same as slugify).
if max_length <= 0:
return string

if len(string) < max_length:
Expand Down
11 changes: 11 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,17 @@ def test_smart_truncate_no_max_length(self):
r = smart_truncate(txt)
self.assertEqual(r, txt)

def test_smart_truncate_nonpositive_max_length(self):
txt = 'hello-world'
self.assertEqual(smart_truncate(txt, max_length=0), txt)
self.assertEqual(smart_truncate(txt, max_length=-1), txt)
self.assertEqual(smart_truncate(txt, max_length=-100), txt)
self.assertEqual(
smart_truncate(txt, max_length=-1, word_boundary=True, separator='-'),
txt,
)
self.assertEqual(slugify('hello world', max_length=-1), 'hello-world')

def test_smart_truncate_no_seperator(self):
txt = '1,000 reasons you are #1'
r = smart_truncate(txt, max_length=100, separator='_')
Expand Down