-
Notifications
You must be signed in to change notification settings - Fork 54
/
slug.go
47 lines (41 loc) 路 832 Bytes
/
slug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package slug
import "unicode"
func Slugify(s string) string {
cutDash := true
emitDash := false
slug := make([]rune, 0, len(s))
for _, r := range s {
if unicode.IsNumber(r) || unicode.IsLetter(r) {
if emitDash && !cutDash {
slug = append(slug, '-')
}
slug = append(slug, unicode.ToLower(r))
emitDash = false
cutDash = false
continue
}
switch r {
case '/', '=':
if len(slug) == 0 || slug[len(slug)-1] != r {
slug = append(slug, r)
}
emitDash = false
cutDash = true
case '-', ',', '.', ' ', '_':
emitDash = true
default:
if name, exists := runename[r]; exists {
if !cutDash {
slug = append(slug, '-')
}
slug = append(slug, []rune(name)...)
cutDash = false
}
emitDash = true
}
}
if len(slug) == 0 {
return "-"
}
return string(slug)
}