-
Notifications
You must be signed in to change notification settings - Fork 19
/
common.go
85 lines (72 loc) · 1.68 KB
/
common.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
package faker
import (
"math/rand"
"github.com/toddlerya/fakerfactory/faker/data"
)
// Check if in lib
func dataCheck(dataVal []string) bool {
var checkOk bool
_, checkOk = data.Data[dataVal[0]]
if len(dataVal) == 2 && checkOk {
_, checkOk = data.Data[dataVal[0]][dataVal[1]]
}
return checkOk
}
// Get Random Value
func getRandValue(dataVal []string) string {
if !dataCheck(dataVal) {
return ""
}
return data.Data[dataVal[0]][dataVal[1]][rand.Intn(len(data.Data[dataVal[0]][dataVal[1]]))]
}
// Replace # with numbers
func replaceWithNumbers(str string) string {
if str == "" {
return str
}
bytestr := []byte(str)
hashtag := []byte("#")[0]
numbers := []byte("0123456789")
for i := 0; i < len(bytestr); i++ {
if bytestr[i] == hashtag {
bytestr[i] = numbers[rand.Intn(9)]
}
}
if bytestr[0] == []byte("0")[0] {
bytestr[0] = numbers[rand.Intn(8)+1]
}
return string(bytestr)
}
// Replace ? with letters
func replaceWithLetters(str string) string {
if str == "" {
return str
}
bytestr := []byte(str)
question := []byte("?")[0]
letters := []byte("abcdefghijklmnopqrstuvwxyz")
for i := 0; i < len(bytestr); i++ {
if bytestr[i] == question {
bytestr[i] = letters[rand.Intn(26)]
}
}
return string(bytestr)
}
// Generate random letter
func randLetter() string {
return string([]byte("abcdefghijklmnopqrstuvwxyz")[rand.Intn(26)])
}
// Generate random integer between min and max
func randIntRange(min, max int) int {
if min == max {
return min
}
return rand.Intn((max+1)-min) + min
}
// Generate random floating-point between min and max
func randFloatRange(min, max float64) float64 {
if min == max {
return min
}
return rand.Float64()*(max-min) + min
}