-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.go
executable file
·210 lines (179 loc) · 4.97 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package helpers
import (
"fmt"
got "html/template"
"strings"
"time"
)
// ARRAYS
// Array takes a set of interface pointers as variadic args, and returns a single array
func Array(args ...interface{}) []interface{} {
return []interface{}{args}
}
// CommaSeparatedArray returns the values as a comma separated string
func CommaSeparatedArray(args []string) string {
result := ""
for _, v := range args {
if len(result) > 0 {
result = fmt.Sprintf("%s,%s", result, v)
} else {
result = v
}
}
return result
}
// MAPS
// Empty returns an empty map[string]interface{} for use as a context
func Empty() map[string]interface{} {
return map[string]interface{}{}
}
// Map sets a map key and return the map
func Map(m map[string]interface{}, k string, v interface{}) map[string]interface{} {
m[k] = v
return m
}
// Set a map key and return an empty string
func Set(m map[string]interface{}, k string, v interface{}) string {
m[k] = v
return "" // Render nothing, we want no side effects
}
// SetIf sets a map key if the given condition is true
func SetIf(m map[string]interface{}, k string, v interface{}, t bool) string {
if t {
m[k] = v
} else {
m[k] = ""
}
return "" // Render nothing, we want no side effects
}
// Append all args to an array, and return that array
func Append(m []interface{}, args ...interface{}) []interface{} {
for _, v := range args {
m = append(m, v)
}
return m
}
// CreateMap - given a set of interface pointers as variadic args, generate and return a map to the values
// This is currently unused as we just use simpler Map add above to add to context
func CreateMap(args ...interface{}) map[string]interface{} {
m := make(map[string]interface{}, 0)
key := ""
for _, v := range args {
if len(key) == 0 {
key = string(v.(string))
} else {
m[key] = v
}
}
return m
}
// Contains returns true if this array of ints contains the given int
func Contains(list []int64, item int64) bool {
for _, b := range list {
if b == item {
return true
}
}
return false
}
// Blank returns true if a string is empty
func Blank(s string) bool {
return len(s) == 0
}
// Exists returns true if this string has a length greater than 0
func Exists(s string) bool {
return len(s) > 0
}
// Time returns a formatted time string given a time and optional format
func Time(time time.Time, formats ...string) got.HTML {
layout := "Jan 2, 2006 at 15:04"
if len(formats) > 0 {
layout = formats[0]
}
value := fmt.Sprintf(time.Format(layout))
return got.HTML(Escape(value))
}
// Ago returns a time string reporting distance from the current date
// of form 5 hours ago
func Ago(t time.Time, formats ...string) string {
duration := time.Since(t)
absDuration := duration
if duration < 0 {
absDuration = -duration
}
hours := absDuration / time.Hour
// Use ago only for past dates
ago := " ago"
if duration < 0 {
ago = ""
}
switch {
case absDuration < time.Minute:
return fmt.Sprintf("%d seconds%s", duration/time.Second, ago)
case absDuration < time.Hour:
return fmt.Sprintf("%d minutes%s", duration/time.Minute, ago)
case absDuration < time.Hour*24:
unit := "hour"
if hours > 1 {
unit = "hours"
}
return fmt.Sprintf("%d %s%s", hours, unit, ago)
default:
unit := "day"
if hours > 48 {
unit = "days"
}
return fmt.Sprintf("%d %s%s", hours/24, unit, ago)
}
}
// Date returns a formatted date string given a time and optional format
// Date format layouts are for the date 2006-01-02
func Date(t time.Time, formats ...string) got.HTML {
//layout := "2006-01-02" // Jan 2, 2006
layout := "Jan 2, 2006"
if len(formats) > 0 {
layout = formats[0]
}
value := fmt.Sprintf(t.Format(layout))
return got.HTML(Escape(value))
}
// UTCDate returns a formatted date string in 2006-01-02
func UTCDate(t time.Time) got.HTML {
return Date(t.UTC(), "2006-01-02")
}
// UTCTime returns a formatted date string in 2006-01-02
func UTCTime(t time.Time) got.HTML {
return Time(t.UTC(), "2006-01-02T15:04:00:00.000Z")
}
// JSONTime returns a formatted date string with format
// suitable for using in a json file
func JSONTime(t time.Time) got.HTML {
return Time(t.UTC(), "2006-01-02T15:04:05Z07:00")
}
// UTCNow returns a formatted date string in 2006-01-02
func UTCNow() got.HTML {
return Date(time.Now().UTC(), "2006-01-02")
}
// YearNow returns a formatted date string for the current year
func YearNow() got.HTML {
return Date(time.Now().UTC(), "2006")
}
// Truncate text to a given length
func Truncate(s string, l int64) string {
return s
}
// CSV escape (replace , with ,,)
func CSV(s got.HTML) string {
return strings.Replace(string(s), ",", ",,", -1)
}
// JSON escapes a string for use in a json template (html template)
func JSON(t string) got.HTML {
// Escape mandatory characters
t = strings.Replace(t, "\r", " ", -1)
t = strings.Replace(t, "\n", " ", -1)
t = strings.Replace(t, "\t", " ", -1)
t = strings.Replace(t, "\\", "\\\\", -1)
t = strings.Replace(t, "\"", "\\\"", -1)
// Because we use html/template escape as temlate.HTML
return got.HTML(t)
}