Skip to content

Commit

Permalink
add: IsSlug function
Browse files Browse the repository at this point in the history
Fix #11
  • Loading branch information
matrixik committed Jul 24, 2017
1 parent f36922d commit e756f2e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
13 changes: 13 additions & 0 deletions slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,16 @@ func smartTruncate(text string) string {
}
return strings.Trim(truncated, "-")
}

// IsSlug returns True if provided text does not contain white characters,
// punctuation, all letters are lower case and only from ASCII range.
// It could contain `-` and `_`.
// All output from slug.Make(text) should pass this test.
func IsSlug(text string) bool {
for _, c := range text {
if (c < 'a' || c > 'z') && c != '-' && c != '_' {
return false
}
}
return true
}
61 changes: 61 additions & 0 deletions slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,33 @@ func TestSlugMakeSmartTruncate(t *testing.T) {
}
}

func TestIsSlug(t *testing.T) {
type args struct {
text string
}
tests := []struct {
name string
args args
want bool
}{
{"some", args{"some"}, true},
{"with -", args{"some-more"}, true},
{"with _", args{"some_more"}, true},
{"upper case", args{"Some-more"}, false},
{"space", args{"some more"}, false},
{"outside ASCII", args{"Dobrosław Żybort"}, false},
{"outside ASCII –", args{"2000–2013"}, false},
{"smile ☺", args{"smile ☺"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsSlug(tt.args.text); got != tt.want {
t.Errorf("IsSlug() = %v, want %v", got, tt.want)
}
})
}
}

func BenchmarkMakeShortAscii(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
Expand Down Expand Up @@ -343,3 +370,37 @@ func BenchmarkSmartTruncateLong(b *testing.B) {
smartTruncate(longStr)
}
}

func BenchmarkIsSlugShort(b *testing.B) {
shortStr := "hello-world"

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
IsSlug(shortStr)
}
}

func BenchmarkIsSlugLong(b *testing.B) {
longStr := "lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit-morbi-" +
"pulvinar-sodales-ultrices-nulla-facilisi-sed-at-vestibulum-erat-ut-" +
"sit-amet-urna-posuere-sagittis-eros-ac-varius-nisi-morbi-ullamcorper-" +
"odio-at-nunc-pulvinar-mattis-vestibulum-rutrum-ante-eu-dictum-mattis,-" +
"elit-risus-finibus-nunc-consectetur-facilisis-eros-leo-ut-sapien-sed-" +
"pulvinar-volutpat-mi-cras-semper-mi-ac-eros-accumsan-at-feugiat-massa-" +
"elementum-morbi-eget-dolor-sit-amet-purus-condimentum-egestas-non-ut-" +
"sapien-duis-feugiat-magna-vitae-nisi-lobortis-quis-finibus-sem-" +
"sollicitudin-pellentesque-eleifend-blandit-ipsum-ut-porta-arcu-" +
"ultricies-et-fusce-vel-ipsum-porta-placerat-diam-ac-consectetur-" +
"magna-nulla-in-porta-sem-suspendisse-commodo-felis-in-molestie-" +
"ultricies-arcu-ipsum-aliquet-turpis-elementum-dapibus-ipsum-lorem-a-" +
"nisl-etiam-varius-imperdiet-placerat-aliquam-euismod-lacus-arcu-" +
"ultrices-hendrerit-est-pellentesque-vel-aliquam-sit-amet-laoreet-leo-" +
"integer-eros-libero-mollis-sed-posuere"

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
IsSlug(longStr)
}
}

0 comments on commit e756f2e

Please sign in to comment.