| 1 |
package testutils |
| 2 |
|
| 3 |
import ( |
| 4 |
"regexp" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// Slug takes a human-readable string and returns a filename-friendly slug. |
| 9 |
func Slug(input string) string { |
| 10 |
// Convert to lowercase |
| 11 |
slug := strings.ToLower(input) |
| 12 |
|
| 13 |
// Replace spaces and underscores with dashes |
| 14 |
slug = strings.ReplaceAll(slug, " ", "_") |
| 15 |
slug = strings.ReplaceAll(slug, "-", "_") |
| 16 |
|
| 17 |
// Remove all non-alphanumeric and non-underscore characters |
| 18 |
re := regexp.MustCompile(`[^a-z0-9\_]+`) |
| 19 |
slug = re.ReplaceAllString(slug, "") |
| 20 |
|
| 21 |
return slug |
| 22 |
} |