-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring.go
More file actions
158 lines (139 loc) · 3.87 KB
/
string.go
File metadata and controls
158 lines (139 loc) · 3.87 KB
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
153
154
155
156
157
158
package gofaker
import (
"math/rand"
)
// Letter will generate a single random lower case ASCII letter
func Letter() string {
return string(randLetter())
}
// Digit will generate a single ASCII digit
func Digit() string {
return string(randDigit())
}
// Numerify will replace # with random numerical values
func Numerify(str string) string {
return replaceWithNumbers(str)
}
// Lexify will replace ? will random generated letters
func Lexify(str string) string {
return replaceWithLetters(str)
}
// ShuffleStrings will randomize a slice of strings
func ShuffleStrings(a []string) {
swap := func(i, j int) {
a[i], a[j] = a[j], a[i]
}
//to avoid upgrading to 1.10 I copied the algorithm
n := len(a)
if n <= 1 {
return
}
//if size is > int32 probably it will never finish, or ran out of entropy
i := n - 1
for ; i > 0; i-- {
j := int(rand.Int31n(int32(i + 1)))
swap(i, j)
}
}
// RandomString will take in a slice of string and return a randomly selected value
func RandomString(a []string) string {
size := len(a)
if size == 0 {
return ""
}
if size == 1 {
return a[0]
}
return a[rand.Intn(size)]
}
func addStringLookup() {
AddFuncLookup("letter", Info{
Display: "Letter",
Category: "string",
Description: "Generate a single random lower case ASCII letter",
Example: "g",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Letter(), nil
},
})
AddFuncLookup("digit", Info{
Display: "Digit",
Category: "string",
Description: "Generate a single random lower case ASCII letter",
Example: "g",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Letter(), nil
},
})
AddFuncLookup("numerify", Info{
Display: "Numerify",
Category: "string",
Description: "Replace # with random numerical values",
Example: "(###)###-#### => (555)867-5309",
Output: "string",
Params: []Param{
{Field: "str", Display: "String", Type: "string", Description: "String value to replace #'s"},
},
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
str, err := info.GetString(m, "str")
if err != nil {
return nil, err
}
return Numerify(str), nil
},
})
AddFuncLookup("lexify", Info{
Display: "Lexify",
Category: "string",
Description: "Replace ? will random generated letters",
Example: "?????@??????.com => billy@mister.com",
Output: "string",
Params: []Param{
{Field: "str", Display: "String", Type: "string", Description: "String value to replace #'s"},
},
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
str, err := info.GetString(m, "str")
if err != nil {
return nil, err
}
return Lexify(str), nil
},
})
AddFuncLookup("shufflestrings", Info{
Display: "Shuffle Strings",
Category: "string",
Description: "Shuffle an array of strings",
Example: "hello,world,whats,up => whats,world,hello,up",
Output: "[]string",
Params: []Param{
{Field: "strs", Display: "Strings", Type: "[]string", Description: "Delimited separated strings"},
},
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
strs, err := info.GetStringArray(m, "strs")
if err != nil {
return nil, err
}
ShuffleStrings(strs)
return strs, nil
},
})
AddFuncLookup("randomstring", Info{
Display: "Random String",
Category: "string",
Description: "Randomly grab one string from array",
Example: "hello,world,whats,up => world",
Output: "[]string",
Params: []Param{
{Field: "strs", Display: "Strings", Type: "[]string", Description: "Delimited separated strings"},
},
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
strs, err := info.GetStringArray(m, "strs")
if err != nil {
return nil, err
}
return RandomString(strs), nil
},
})
}