Last active
August 29, 2015 14:25
-
-
Save dvliman/31928cfe18e0e026a1b9 to your computer and use it in GitHub Desktop.
slugify for permalink
This file contains hidden or 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
// utils.go | |
func slugify(input string) string { | |
// 1) trim and lower case input string | |
// 2) for every subsequent whitespace, collapse to just one whitespace | |
// 3) for every whitespace, replace with '-'; including from previous step 2) | |
// 4) for every non alphanumeric character, replace with empty space | |
} | |
// utils_test.go | |
func TestSlugify(t *testing.T) { | |
tests := map[string]string { | |
"Macy's": "macys", | |
"Neiman Marcus": "neiman-marcus", | |
"crazy branDD with whitespaces": "crazy-brandd-with-whitespaces", | |
" trim-it-please ": "trim-it-please" | |
} | |
for input, output := range tests { | |
if input != ouput { | |
t.Errorf("input: %v, output: %v, expected: %v", input, slugify(input), output) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment