From 5ae7f860576f9704634c5e85179abc28a2cb3342 Mon Sep 17 00:00:00 2001 From: Romain Poirot Date: Mon, 1 Aug 2022 15:36:59 +0200 Subject: [PATCH 1/6] fix edge case in truncate function allowing too long slugs --- slug.go | 20 +++++++------------- slug_test.go | 1 + 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/slug.go b/slug.go index 14acce7..a336d4d 100644 --- a/slug.go +++ b/slug.go @@ -150,21 +150,15 @@ func smartTruncate(text string) string { 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 - 1; 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, diff --git a/slug_test.go b/slug_test.go index baace64..4c920ee 100644 --- a/slug_test.go +++ b/slug_test.go @@ -273,6 +273,7 @@ func TestSlugMakeSmartTruncate(t *testing.T) { {"DOBROSLAWZYBORT", 100, "dobroslawzybort"}, {"Dobroslaw Zybort", 100, "dobroslaw-zybort"}, {"Dobroslaw Zybort", 12, "dobroslaw"}, + {"Dobroslaw Zybort", 15, "dobroslaw"}, {" Dobroslaw Zybort ?", 12, "dobroslaw"}, {"Ala ma 6 kotów.", 10, "ala-ma-6"}, {"Dobrosław Żybort", 5, "dobro"}, From 05a1da2485b26a6a5e50b13990b67b7e25cf9131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dobros=C5=82aw=20=C5=BBybort?= Date: Tue, 27 Sep 2022 23:19:34 +0200 Subject: [PATCH 2/6] Update slug_test.go --- slug_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/slug_test.go b/slug_test.go index 4c920ee..f8b9f83 100644 --- a/slug_test.go +++ b/slug_test.go @@ -274,6 +274,7 @@ func TestSlugMakeSmartTruncate(t *testing.T) { {"Dobroslaw Zybort", 100, "dobroslaw-zybort"}, {"Dobroslaw Zybort", 12, "dobroslaw"}, {"Dobroslaw Zybort", 15, "dobroslaw"}, + {"Dobroslaw Zybort", 16, "dobroslaw-zybort"}, {" Dobroslaw Zybort ?", 12, "dobroslaw"}, {"Ala ma 6 kotów.", 10, "ala-ma-6"}, {"Dobrosław Żybort", 5, "dobro"}, From 9d02f6196401e53be0da945a371594f57a4cc182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dobros=C5=82aw=20=C5=BBybort?= Date: Tue, 27 Sep 2022 23:24:56 +0200 Subject: [PATCH 3/6] Add testcase for edge casf --- slug_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/slug_test.go b/slug_test.go index f8b9f83..abeb296 100644 --- a/slug_test.go +++ b/slug_test.go @@ -275,6 +275,7 @@ func TestSlugMakeSmartTruncate(t *testing.T) { {"Dobroslaw Zybort", 12, "dobroslaw"}, {"Dobroslaw Zybort", 15, "dobroslaw"}, {"Dobroslaw Zybort", 16, "dobroslaw-zybort"}, + {"Dobroslaw Zybort", 17, "dobroslaw-zybort"}, {" Dobroslaw Zybort ?", 12, "dobroslaw"}, {"Ala ma 6 kotów.", 10, "ala-ma-6"}, {"Dobrosław Żybort", 5, "dobro"}, From 133079cd8453025964740da2e8a71ca316195f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dobros=C5=82aw=20=C5=BBybort?= Date: Tue, 27 Sep 2022 23:51:37 +0200 Subject: [PATCH 4/6] Improve TestSlugMakeSmartTruncate cases --- slug_test.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/slug_test.go b/slug_test.go index 21dcb89..645f4e7 100644 --- a/slug_test.go +++ b/slug_test.go @@ -283,15 +283,33 @@ 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", 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", 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}, } From fb7006817aa398102575f7ca467a78e59dfdcfd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dobros=C5=82aw=20=C5=BBybort?= Date: Fri, 30 Sep 2022 19:57:42 +0200 Subject: [PATCH 5/6] Even more smart truncate test cases --- slug_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/slug_test.go b/slug_test.go index 645f4e7..88cb2fa 100644 --- a/slug_test.go +++ b/slug_test.go @@ -291,9 +291,14 @@ func TestSlugMakeSmartTruncate(t *testing.T) { {"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}, @@ -305,6 +310,7 @@ func TestSlugMakeSmartTruncate(t *testing.T) { {"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}, From c05c02da15193eb2e400faffb86b63bafeadee72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dobros=C5=82aw=20=C5=BBybort?= Date: Fri, 30 Sep 2022 20:16:43 +0200 Subject: [PATCH 6/6] Fix bug in smast truncate --- slug.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slug.go b/slug.go index c33acc1..5a2b284 100644 --- a/slug.go +++ b/slug.go @@ -160,14 +160,14 @@ func SubstituteRune(s string, sub map[rune]string) string { } func smartTruncate(text string) string { - if len(text) < MaxLength { + if len(text) <= MaxLength { return text } // 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 - 1; i >= 0; i-- { + for i := MaxLength; i >= 0; i-- { if text[i] == '-' { return text[:i] }