Skip to content

Instantly share code, notes, and snippets.

@dvliman
Last active August 29, 2015 14:25
Show Gist options
  • Save dvliman/31928cfe18e0e026a1b9 to your computer and use it in GitHub Desktop.
Save dvliman/31928cfe18e0e026a1b9 to your computer and use it in GitHub Desktop.
slugify for permalink
// 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