forked from jaswdr/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaker.go
287 lines (231 loc) · 6.6 KB
/
faker.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package faker
import (
"fmt"
"math"
"math/rand"
"strconv"
"strings"
"time"
)
// Faker is the Generator struct for Faker
type Faker struct {
Generator *rand.Rand
}
// RandomDigit returns a fake random digit for Faker
func (f Faker) RandomDigit() int {
return f.Generator.Int() % 10
}
// RandomDigitNot returns a fake random digit for Faker that is not in a list of ignored
func (f Faker) RandomDigitNot(ignore ...int) int {
inSlice := func(el int, list []int) bool {
for i := range list {
if i == el {
return true
}
}
return false
}
for {
current := f.RandomDigit()
if inSlice(current, ignore) {
return current
}
}
}
// RandomDigitNotNull returns a fake random digit that is not null for Faker
func (f Faker) RandomDigitNotNull() int {
return f.Generator.Int()%8 + 1
}
// RandomNumber returns a fake random integer number for Faker
func (f Faker) RandomNumber(size int) int {
if size == 1 {
return f.RandomDigit()
}
var min int = int(math.Pow10(size - 1))
var max int = int(math.Pow10(size)) - 1
return f.IntBetween(min, max)
}
// RandomFloat returns a fake random float number for Faker
func (f Faker) RandomFloat(maxDecimals, min, max int) float64 {
s := fmt.Sprintf("%d.%d", f.IntBetween(min, max-1), f.IntBetween(1, maxDecimals))
value, _ := strconv.ParseFloat(s, 10)
return value
}
// Int returns a fake Int number for Faker
func (f Faker) Int() int {
maxU := ^uint(0) >> 1
max := int(maxU)
min := -max - 1
return f.IntBetween(min, max)
}
// Int64 returns a fake Int64 number for Faker
func (f Faker) Int64() int64 {
return int64(f.Int())
}
// Int32 returns a fake Int32 number for Faker
func (f Faker) Int32() int32 {
return int32(f.Int())
}
// IntBetween returns a fake Int between a given minimum and maximum values for Faker
func (f Faker) IntBetween(min, max int) int {
diff := max - min
if diff == 0 {
return min
}
return min + (f.Generator.Int() % diff)
}
// Int64Between returns a fake Int64 between a given minimum and maximum values for Faker
func (f Faker) Int64Between(min, max int64) int64 {
return int64(f.IntBetween(int(min), int(max)))
}
// Int32Between returns a fake Int32 between a given minimum and maximum values for Faker
func (f Faker) Int32Between(min, max int32) int32 {
return int32(f.IntBetween(int(min), int(max)))
}
// Letter returns a fake single letter for Faker
func (f Faker) Letter() string {
return f.RandomLetter()
}
// RandomLetter returns a fake random string with a random number of letters for Faker
func (f Faker) RandomLetter() string {
return fmt.Sprintf("%c", f.IntBetween(97, 122))
}
// RandomStringElement returns a fake random string element from a given list of strings for Faker
func (f Faker) RandomStringElement(s []string) string {
i := f.IntBetween(0, len(s))
return s[i]
}
// RandomIntElement returns a fake random int element form a given list of ints for Faker
func (f Faker) RandomIntElement(a []int) int {
i := f.IntBetween(0, len(a))
return a[i]
}
// ShuffleString returns a fake shuffled string from a given string for Faker
func (f Faker) ShuffleString(s string) string {
orig := strings.Split(s, "")
dest := make([]string, len(orig))
for i := 0; i < len(orig); i++ {
dest[i] = orig[len(orig)-i-1]
}
return strings.Join(dest, "")
}
// Numerify returns a fake string that replace all "#" characters with numbers from a given string for Faker
func (f Faker) Numerify(in string) (out string) {
for _, c := range strings.Split(in, "") {
if c == "#" {
c = strconv.Itoa(f.RandomDigit())
}
out = out + c
}
return
}
// Lexify returns a fake string that replace all "?" characters with random letters from a given string for Faker
func (f Faker) Lexify(in string) (out string) {
for _, c := range strings.Split(in, "") {
if c == "?" {
c = f.RandomLetter()
}
out = out + c
}
return
}
// Bothify returns a fake string that apply Lexify() and Numerify() on a given string for Faker
func (f Faker) Bothify(in string) (out string) {
out = f.Lexify(in)
out = f.Numerify(out)
return
}
// Asciify returns a fake string that replace all "*" characters with random ASCII values from a given string for Faker
func (f Faker) Asciify(in string) (out string) {
for _, c := range strings.Split(in, "") {
if c == "*" {
c = fmt.Sprintf("%c", f.IntBetween(97, 126))
}
out = out + c
}
return
}
// Bool returns a fake bool for Faker
func (f Faker) Bool() bool {
return f.Boolean().Bool()
}
// BoolWithChance returns true with a given percentual chance that the value is true, otherwise returns false
func (f Faker) BoolWithChance(chanceTrue int) bool {
return f.Boolean().BoolWithChance(chanceTrue)
}
// Boolean returns a fake Boolean instance for Faker
func (f Faker) Boolean() Boolean {
return Boolean{&f}
}
// Lorem returns a fake Lorem instance for Faker
func (f Faker) Lorem() Lorem {
return Lorem{&f}
}
// Person returns a fake Person instance for Faker
func (f Faker) Person() Person {
return Person{&f}
}
// Address returns a fake Address instance for Faker
func (f Faker) Address() Address {
return Address{&f}
}
// Phone returns a fake Phone instance for Faker
func (f Faker) Phone() Phone {
return Phone{&f}
}
// Company returns a fake Company instance for Faker
func (f Faker) Company() Company {
return Company{&f}
}
// Time returns a fake Time instance for Faker
func (f Faker) Time() Time {
return Time{&f}
}
// Internet returns a fake Internet instance for Faker
func (f Faker) Internet() Internet {
return Internet{&f}
}
// UserAgent returns a fake UserAgent instance for Faker
func (f Faker) UserAgent() UserAgent {
return UserAgent{&f}
}
// Payment returns a fake Payment instance for Faker
func (f Faker) Payment() Payment {
return Payment{&f}
}
// MimeType returns a fake MimeType instance for Faker
func (f Faker) MimeType() MimeType {
return MimeType{&f}
}
// Color returns a fake Color instance for Faker
func (f Faker) Color() Color {
return Color{&f}
}
// UUID returns a fake UUID instance for Faker
func (f Faker) UUID() UUID {
return UUID{&f}
}
// Image returns a fake Image instance for Faker
func (f Faker) Image() Image {
return Image{&f}
}
// File returns a fake File instance for Faker
func (f Faker) File() File {
return File{&f}
}
// YouTube returns a fake YouTube instance for Faker
func (f Faker) YouTube() YouTube {
return YouTube{&f}
}
// New returns a new instance of Faker instance with a random seed
func New() (f Faker) {
seed := rand.NewSource(time.Now().Unix())
f = NewWithSeed(seed)
return
}
// NewWithSeed returns a new instance of Faker instance with a given seed
func NewWithSeed(src rand.Source) (f Faker) {
generator := rand.New(src)
f = Faker{Generator: generator}
return
}