4
4
"crypto/md5"
5
5
"fmt"
6
6
"strings"
7
+ "unicode"
7
8
)
8
9
9
10
// use SanitizeNameV2
@@ -28,23 +29,19 @@ func SanitizeName(name string) string {
28
29
}
29
30
30
31
func SanitizeNameV2 (name string ) string {
31
- name = strings .Replace (name , "*" , "-" , - 1 )
32
- name = strings .Replace (name , "/" , "-" , - 1 )
33
- name = strings .Replace (name , "." , "-" , - 1 )
34
- name = strings .Replace (name , "[" , "" , - 1 )
35
- name = strings .Replace (name , "]" , "" , - 1 )
36
- name = strings .Replace (name , ":" , "-" , - 1 )
37
- name = strings .Replace (name , "_" , "-" , - 1 )
38
- name = strings .Replace (name , " " , "-" , - 1 )
39
- name = strings .Replace (name , "\n " , "" , - 1 )
40
- name = strings .Replace (name , "\" " , "" , - 1 )
41
- name = strings .Replace (name , "'" , "" , - 1 )
32
+ name = strings .Map (func (r rune ) rune {
33
+ switch r {
34
+ case '*' , '/' , '.' , ':' , '_' , ' ' :
35
+ return '-'
36
+ case '[' , ']' , '\n' , '"' , '\'' :
37
+ return - 1
38
+ }
39
+ return unicode .ToLower (r )
40
+ }, name )
42
41
if len (name ) > 63 {
43
42
hash := md5 .Sum ([]byte (name ))
44
43
name = fmt .Sprintf ("%s-%x" , name [:31 ], hash )
45
44
name = name [:63 ]
46
45
}
47
- name = strings .Replace (name , "." , "-" , - 1 )
48
- name = strings .ToLower (name )
49
46
return name
50
47
}
0 commit comments