File: slug.go

package info (click to toggle)
nerdlog 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,296 kB
  • sloc: sh: 1,004; makefile: 85
file content (22 lines) | stat: -rw-r--r-- 511 bytes parent folder | download
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
}