Skip to content

Commit

Permalink
Fix edge case in truncate function allowing too long slugs (#74)
Browse files Browse the repository at this point in the history
Fix #72

* Add more TestSlugMakeSmartTruncate cases

Co-authored-by: Romain Poirot <romain.poirot@gitguardian.com>
Co-authored-by: Dobrosław Żybort <matrixik@gmail.com>
  • Loading branch information
3 people committed Sep 30, 2022
1 parent 452645f commit 6732d2a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
22 changes: 8 additions & 14 deletions slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,19 @@ func SubstituteRune(s string, sub map[rune]string) string {
}

func smartTruncate(text string) string {
if len(text) < MaxLength {
if len(text) <= MaxLength {
return text
}

var truncated string
words := strings.SplitAfter(text, "-")
// If MaxLength is smaller than length of the first word return word
// truncated after MaxLength.
if len(words[0]) > MaxLength {
return words[0][:MaxLength]
}
for _, word := range words {
if len(truncated)+len(word)-1 <= MaxLength {
truncated = truncated + word
} else {
break
// If slug is too long, we need to find the last '-' before MaxLength, and
// we cut there.
// If we don't find any, we have only one word, and we cut at MaxLength.
for i := MaxLength; i >= 0; i-- {
if text[i] == '-' {
return text[:i]
}
}
return strings.Trim(truncated, "-")
return text[:MaxLength]
}

// IsSlug returns True if provided text does not contain white characters,
Expand Down
33 changes: 30 additions & 3 deletions slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,39 @@ func TestSlugMakeSmartTruncate(t *testing.T) {
want string
smartTruncate bool
}{
{"DOBROSLAWZYBORT", 100, "dobroslawzybort", true},
{"Dobroslaw Zybort", 100, "dobroslaw-zybort", true},
{"Dobrosław Żybort", 5, "dobro", true},
{"Dobroslaw Zybort", 9, "dobroslaw", true},
{"Dobroslaw Zybort", 12, "dobroslaw", true},
{"Dobroslaw Zybort", 15, "dobroslaw", true},
{"Dobroslaw Zybort", 16, "dobroslaw-zybort", true},
{"Dobroslaw Zybort", 17, "dobroslaw-zybort", true},
{"Dobroslaw Zybort", 100, "dobroslaw-zybort", true},
{"abc", 2, "ab", true},
{"-abc", 2, "ab", true},
{"abc-", 2, "ab", true},
{"abc", 3, "abc", true},
{"-abc", 3, "abc", true},
{"abc", 4, "abc", true},
{"abc-", 4, "abc", true},
{"-abc-", 4, "abc", true},
{"----abc----", 4, "abc", true},
{"abc-de", 4, "abc", true},
{"abc-de", 5, "abc", true},
{"abc-de", 6, "abc-de", true},
{"abc-de", 7, "abc-de", true},
{"abc-de-fg", 6, "abc-de", true},
{"abc-de-fg", 7, "abc-de", true},
{"abc-de-fg", 8, "abc-de", true},
{"abc-de-fg", 9, "abc-de-fg", true},
{"abc-de-fg", 10, "abc-de-fg", true},

{"DOBROSLAWZYBORT", 9, "dobroslaw", true},
{"DOBROSLAWZYBORT", 15, "dobroslawzybort", true},
{"DOBROSLAWZYBORT", 100, "dobroslawzybort", true},
{" Dobroslaw Zybort ?", 12, "dobroslaw", true},
{"Ala ma 6 kotów.", 10, "ala-ma-6", true},
{"Dobrosław Żybort", 5, "dobro", true},

// No smart truncate
{"Long branch-name", 14, "long-branch-na", false},
{"Long branch-name", 12, "long-branch", false},
}
Expand Down

0 comments on commit 6732d2a

Please sign in to comment.