forked from volatiletech/abcweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strmangle.go
56 lines (46 loc) · 1.38 KB
/
strmangle.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
package strmangle
import (
"math/rand"
"strings"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
// RandString generates a random string of length len using the chars in letterRunes
func RandString(length int) string {
b := make([]rune, length)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
// EnvAppName converts the app name to an environment variable compatible name, eg:
// "My-app Name" -> "MY_APP_NAME"
func EnvAppName(s string) string {
return strings.ToUpper(replaceNonAlpha(s, '_'))
}
// DBAppName converts the app name to a database compatible name, eg:
// "My-app Name" -> "my_app_name"
func DBAppName(s string) string {
return strings.ToLower(replaceNonAlpha(s, '_'))
}
// SanitizeAppName converts the app name to a github sanitized name, eg:
// "My_app@Name" -> "My-app-Name"
func SanitizeAppName(s string) string {
return replaceNonAlpha(s, '-')
}
// replaceNonAlpha replaces non alphabet characters with the replace byte.
func replaceNonAlpha(s string, replace byte) string {
byts := []byte(s)
newByts := make([]byte, len(byts))
for i := 0; i < len(byts); i++ {
if byts[i] < 'A' || (byts[i] > 'Z' && byts[i] < 'a') || byts[i] > 'z' {
newByts[i] = replace
} else {
newByts[i] = byts[i]
}
}
return string(newByts)
}