Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix edge case in truncate function allowing too long slugs #74

Merged
merged 7 commits into from
Sep 30, 2022
Merged
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
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