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