27 lines
459 B
Go
27 lines
459 B
Go
package slug
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
func Make(raw string) string {
|
|
raw = strings.TrimSpace(strings.ToLower(raw))
|
|
var builder strings.Builder
|
|
lastDash := false
|
|
|
|
for _, r := range raw {
|
|
switch {
|
|
case unicode.IsLetter(r), unicode.IsDigit(r):
|
|
builder.WriteRune(r)
|
|
lastDash = false
|
|
default:
|
|
if !lastDash && builder.Len() > 0 {
|
|
builder.WriteRune('-')
|
|
lastDash = true
|
|
}
|
|
}
|
|
}
|
|
|
|
return strings.Trim(builder.String(), "-")
|
|
}
|