-
Notifications
You must be signed in to change notification settings - Fork 11
/
string.go
152 lines (125 loc) · 3.82 KB
/
string.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package faker
import (
"regexp"
"strings"
)
const digits = "0123456789"
const lowerLetters = "abcdefghijklmnopqrstuvwxyz"
const upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const charset = digits + lowerLetters + upperLetters
// StringWithSize will build a random string of length size.
func StringWithSize(size int) string {
return stringWithSize(size, charset)
}
// String will build a random string of length between 1 and 8.
func String() string {
return StringWithSize(IntInRange(1, 8))
}
// DigitsWithSize will build a random string of only digits of length size.
func DigitsWithSize(size int) string {
return stringWithSize(size, digits)
}
// Digits will build a random string of only digits of length between 1 and 8.
func Digits() string {
return DigitsWithSize(IntInRange(1, 8))
}
// LettersWithSize will build a random string of only letters of length size.
func LettersWithSize(size int) string {
return stringWithSize(size, lowerLetters+upperLetters)
}
// Letters will build a random string of only letters of length between 1 and 8.
func Letters() string {
return LettersWithSize(IntInRange(1, 8))
}
// Lexify will replace all occurrences of "?" in str with a random letter.
func Lexify(str string) string {
return replaceChar(str, "?", func() string { return LettersWithSize(1) })
}
// Numerify will replace all occurrences of "?" in str with a random digit.
func Numerify(str string) string {
return replaceChar(str, "?", func() string { return DigitsWithSize(1) })
}
// Parameterize replaces special characters in str so that it may be used as part of a 'pretty' URL.
func Parameterize(str string) string {
notAlphaNumRegExp := regexp.MustCompile("[^A-Za-z0-9]+")
firstLastDashRegexp := regexp.MustCompile("^-|-$")
parameterizeString := notAlphaNumRegExp.ReplaceAllString(str, "-")
parameterizeString = firstLastDashRegexp.ReplaceAllString(parameterizeString, "")
return strings.ToLower(parameterizeString)
}
// Pick returns a random string among those passed as parameters.
func Pick(pool ...string) string {
if len(pool) == 0 {
return ""
}
i := IntInRange(0, len(pool)-1)
return pool[i]
}
// Private functions
func stringWithSize(size int, charset string) string {
b := make([]byte, size)
for i := range b {
b[i] = charset[random.Intn(len(charset))]
}
return string(b)
}
func replaceChar(str, chr string, fn func() string) string {
r := str
count := strings.Count(str, chr)
for i := 0; i < count; i++ {
r = strings.Replace(r, chr, fn(), 1)
}
return r
}
// Builder functions
func stringWithSizeBuilder(params ...string) (interface{}, error) {
size, err := paramsToInt(params...)
if err != nil {
return nil, err
}
return StringWithSize(size), nil
}
func stringBuilder(params ...string) (interface{}, error) {
return String(), nil
}
func digitsWithSizeBuilder(params ...string) (interface{}, error) {
size, err := paramsToInt(params...)
if err != nil {
return nil, err
}
return DigitsWithSize(size), nil
}
func digitsBuilder(params ...string) (interface{}, error) {
return Digits(), nil
}
func lettersWithSizeBuilder(params ...string) (interface{}, error) {
size, err := paramsToInt(params...)
if err != nil {
return nil, err
}
return LettersWithSize(size), nil
}
func lettersBuilder(params ...string) (interface{}, error) {
return Letters(), nil
}
func lexifyBuilder(params ...string) (interface{}, error) {
if len(params) != 1 {
return nil, parametersError(nil)
}
return Lexify(params[0]), nil
}
func numerifyBuilder(params ...string) (interface{}, error) {
if len(params) != 1 {
return nil, parametersError(nil)
}
return Numerify(params[0]), nil
}
func parameterizeBuilder(params ...string) (interface{}, error) {
if len(params) != 1 {
return nil, parametersError(nil)
}
return Parameterize(params[0]), nil
}
func pickBuilder(params ...string) (interface{}, error) {
return Pick(params...), nil
}