-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
188 lines (166 loc) · 4.16 KB
/
helpers.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
package helpers
import (
"errors"
"math"
"strconv"
"strings"
"time"
"unicode"
)
// TextFromNumber Returns the text based form of a number.
// e.g. 1 would return One
func TextFromNumber(num int) string {
switch num {
case 1:
return ""
case 2:
return "Two"
case 3:
return "Three"
case 4:
return "Four"
case 5:
return "Five"
case 6:
return "Six"
case 7:
return "Seven"
case 8:
return "Eight"
case 9:
return "Nine"
case 10:
return "Ten"
case 11:
return "Eleven"
case 12:
return "Twelve"
case 13:
return "Thirteen"
case 14:
return "Forteen"
case 15:
return "Fifteen"
case 16:
return "Sixteen"
case 17:
return "Seventeen"
case 18:
return "Eighteen"
case 19:
return "Nineteen"
}
return "Zero"
}
func padTimes(str string, n int) (out string) {
for i := 0; i < n; i++ {
out += str
}
return
}
// Left left-pads the string with pad up to len runes
// len may be exceeded if
func PadLeft(str string, length int, pad string) string {
return padTimes(pad, length-len(str)) + str
}
// Right right-pads the string with pad up to len runes
func PadRight(str string, length int, pad string) string {
return str + padTimes(pad, length-len(str))
}
func PadIntLeft(num int, len int, pad string) string {
str := strconv.Itoa(num)
return PadLeft(str, len, pad)
}
func Round(val float64) int {
if val < 0 {
return int(val - 0.5)
}
return int(val + 0.5)
}
// ToFixed rounds to the specified decimal place
func ToFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(Round(num*output)) / output
}
func KebabCase(in string) string {
out := SnakeCase(in)
out = strings.Replace(out, "_", "-", -1)
return out
}
func SnakeCase(in string) string {
in = strings.Replace(in, " ", "_", -1)
runes := []rune(in)
length := len(runes)
var out []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
out = append(out, '_')
}
out = append(out, unicode.ToLower(runes[i]))
}
return strings.Replace(string(out), "__", "_", -1)
}
func SplitTitleCase(in string) string {
out := titleCase(in, " ")
return out
}
func TitleCase(in string) string {
out := titleCase(in, "")
return out
}
func titleCase(in string, sep string) string {
out := SnakeCase(in)
words := strings.Split(out, "_")
for i, word := range words {
words[i] = strings.Title(word)
}
out = strings.Join(words, sep)
return out
}
func Substring(str string, length int) string {
if len(str) > length {
return strings.Join(strings.Split(str, "")[:length], "")
}
return str
}
func TSVSearch(str string) string {
if str == "" {
return str
}
return strings.Join(strings.Split(str, " "), ":* & ") + ":*"
}
func TimeInLoc(t time.Time, loc string) (time.Time, error) {
l, err := time.LoadLocation(loc)
if err != nil {
return t, err
}
return t.In(l), nil
}
func FormatMoney(dec float64) string {
return "$" + strconv.FormatFloat(ToFixed(dec, 2), 'f', 2, 64)
}
func appendSiteULID(siteULID string, whereSql string, args ...interface{}) (string, []interface{}, error) {
if !strings.Contains(whereSql, "$SITEULID") {
return whereSql, args, errors.New("No $SITEULID placeholder defined")
}
args = append(args, siteULID)
position := len(args)
if strings.Contains(whereSql, ".$SITEULID") {
newSQL := strings.Split(whereSql, "$SITEULID")[0]
replaceSQLParts := strings.Split(newSQL, " ")
replaceSQLTablePrefix := replaceSQLParts[len(replaceSQLParts)-1]
whereSql = strings.Replace(whereSql, replaceSQLTablePrefix+"$SITEULID", " and "+replaceSQLTablePrefix+"site_ulid = $"+strconv.Itoa(position), -1)
} else if strings.Contains(whereSql, "$SITEULID") {
whereSql = strings.Replace(whereSql, "$SITEULID", " site_ulid = $"+strconv.Itoa(position), -1)
} else {
whereSql += " and site_ulid = $" + strconv.Itoa(position)
}
return whereSql, args, nil
}
func BastardizeSql(siteULID string, whereSql string, args ...interface{}) (string, []interface{}, error) {
whereSql = strings.Trim(whereSql, " ")
if !strings.HasPrefix(strings.ToLower(whereSql), "where") {
whereSql += "where "
}
return appendSiteULID(siteULID, " "+whereSql+" ", args)
}