-
Notifications
You must be signed in to change notification settings - Fork 1
/
sanitize.go
75 lines (62 loc) · 1.63 KB
/
sanitize.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Inspired by:
//https://go.googlesource.com/text/+/master/runes/example_test.go
//https://www.dotnetperls.com/replace-go
package main
import (
"fmt"
"log"
"os"
"regexp"
"strings"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
func removeIllFormed(input string) (output string) {
output, _, _ = transform.String(runes.ReplaceIllFormed(), input)
return output
}
func toLower(input string) (output string) {
output = strings.ToLower(input)
return output
}
func replaceNonAlphaNum(input string) (output string) {
replaceNonAlphaNum := runes.Map(func(r rune) rune {
if !unicode.Is(unicode.Latin, r) && !unicode.IsDigit(r) {
return '-'
}
return r
})
output, _, _ = transform.String(replaceNonAlphaNum, input)
return output
}
func removeAccents(input string) (output string) {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
s, _, _ := transform.String(t, input)
r := strings.NewReplacer("ł", "l", "Ł", "L")
output = r.Replace(s)
return output
}
func dedupHyp(input string) (output string) {
reg, err := regexp.Compile("-{2,}")
if err != nil {
log.Fatal(err)
}
output = reg.ReplaceAllString(input, "-")
return output
}
func trimEnds(input string) (output string) {
output = strings.TrimFunc(input, func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsDigit(r)
})
return output
}
func sanitize(input string) (output string) {
output = trimEnds(dedupHyp(replaceNonAlphaNum(removeAccents(toLower(removeIllFormed(input))))))
return output
}
func main() {
input := strings.Join(os.Args[1:], "-")
fmt.Println(sanitize(input))
}