-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
148 lines (125 loc) · 3.51 KB
/
util.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
package faker
import (
"errors"
"math/rand"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/Arinzeokeke/tr"
)
type strslice []string
func replaceSymbols(s string) string {
alpha := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
str := ""
for _, v := range alpha {
if strings.Compare(v, "#") == 0 {
str += strconv.Itoa(random(10))
} else if strings.Compare(v, "?") == 0 {
str += alpha[random(len(alpha))]
} else if strings.Compare(v, "*") == 0 {
rn := random(2)
if rn == 0 {
str += strconv.Itoa(random(10))
} else {
str += alpha[random(len(alpha))]
}
} else {
str += v
}
}
return str
}
// random returns a non-negative pseudo-random number in [0,n).
// It's an abstraction over rand.Intn() with a unix-time sourse
func random(i int) int {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
return r.Intn(i)
}
// randomIntRange returns a non-negative pseudo-random number in a defined range.
func randomIntRange(min, max int) int {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
return r.Intn(max-min) + min
}
// randomFloatRange returns, as float64, a non-negative pseudo-random number in [min, max).
func randomFloatRange(min, max int) float64 {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
return float64(r.Intn(max-min)) + float64(min) + r.Float64()
}
func directoryExists(dir string) bool {
dir, _ = filepath.Abs(dir)
if _, err := os.Stat(dir); os.IsNotExist(err) {
return false
}
return true
}
func getList(n *tr.Engine, q, d string) ([]string, error) {
w, err := n.Tr(q)
if err != nil {
w, err = n.Lang(d).Tr(q)
}
if err != nil {
return nil, err
}
return strings.Split(w, "\n"), nil
}
// Find the first index at which an element exists in the array
func (slice strslice) indexOf(searchElement string) int {
for index, val := range slice {
if val == searchElement {
return index
}
}
return -1
}
// returns a random elemnet from an array of strings
func selectElement(arr []string) string {
return arr[random(len(arr))]
}
func slugify(word string) string {
re := regexp.MustCompile(" ")
rex := regexp.MustCompile(`[^\w\.\-]+`)
replaceHyphen := re.ReplaceAllString(word, "-")
replaceBlank := rex.ReplaceAllString(replaceHyphen, "")
return replaceBlank
}
// returns a 4-digit hash.
func randHash() string {
hash := ""
for i := 0; i < 4; i++ {
hash += selectElement([]string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"})
}
return hash
}
// Replace occurences of {{ adjective|verb|abbreviation|noun }} in a sring with a value.
// For example:
// We need to {{ verbtheadjective
// Weneedtooverridethewireless }}
func mustache(str string, data map[string]string) string {
for key, val := range data {
re := regexp.MustCompile("{{" + key + "}}")
str = re.ReplaceAllString(str, val)
}
return str
}
// Call a particular function by name from a map of functions and names.
// Courtesy of https://mikespook.com/2012/07/function-call-by-name-in-golang/
func callFunc(m map[string]interface{}, functionName string, params ...interface{}) (result []reflect.Value, err error) {
f := reflect.ValueOf(m[functionName])
if len(params) != f.Type().NumIn() {
err = errors.New("The number of params is not adapted")
return nil, err
}
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
result = f.Call(in)
return
}