Last active
December 24, 2015 23:49
-
-
Save Luit/6883221 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
func Slugify(title string) string { | |
var slug []byte | |
for _, x := range title { | |
switch { | |
case ('a' <= x && x <= 'z') || ('0' <= x && x <= '9'): | |
slug = append(slug, byte(x)) | |
case 'A' <= x && x <= 'Z': | |
slug = append(slug, byte(x)+0x20) | |
case x == '-' || x == ' ': | |
slug = append(slug, '-') | |
} | |
} | |
return string(slug) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import ( | |
"testing" | |
) | |
type ExpectedResult struct { | |
title string | |
slug string | |
} | |
var expectedResults = []ExpectedResult{ | |
ExpectedResult{ | |
title: "Fix low wifi speed on Linux (Ubuntu) with chip Atheros AR9285", | |
slug: "fix-low-wifi-speed-on-linux-ubuntu-with-chip-atheros-ar9285", | |
}, | |
ExpectedResult{ | |
title: "Python and Scala smoke the peace pipe", | |
slug: "python-and-scala-smoke-the-peace-pipe", | |
}, | |
ExpectedResult{ | |
title: "Graphite, Carbon and Diamond", | |
slug: "graphite-carbon-and-diamond", | |
}, | |
ExpectedResult{ | |
title: "Here I go PyGrunn'13!", | |
slug: "here-i-go-pygrunn13", | |
}, | |
ExpectedResult{ | |
title: "How-to install GNOME 3 instead of Unity on Ubuntu 11.04", | |
slug: "how-to-install-gnome-3-instead-of-unity-on-ubuntu-1104", | |
}, | |
} | |
func TestSlugify(t *testing.T) { | |
for testNumber, testExpected := range expectedResults { | |
title := testExpected.title | |
expectedSlug := testExpected.slug | |
if result := Slugify(title); result != expectedSlug { | |
t.Errorf("#%d (%s)\n+++ %s\n--- %s", testNumber, title, result, expectedSlug) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment