-
Notifications
You must be signed in to change notification settings - Fork 0
/
slug.go
43 lines (36 loc) · 809 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
package latinify
import (
"regexp"
"strings"
)
type stringTransform func(string) string
func apply(funcs ...stringTransform) func(string) string {
return func(src string) (res string) {
res = src
for _, f := range funcs {
res = f(res)
}
return
}
}
// Slugify transform a source folder in a no spacede ascii string
func Slugify(source string) (string, error) {
latin, err := String(source)
if err != nil {
return "", err
}
spacers := regexp.MustCompile(`\s+`)
notAWord := regexp.MustCompile(`[^a-zA-Z0-9\-_]+`)
transformers := apply(
strings.ToLower,
strings.TrimSpace,
func(src string) string {
return spacers.ReplaceAllString(src, "-")
},
func(src string) string {
return notAWord.ReplaceAllString(src, "")
},
)
res := transformers(latin)
return res, nil
}