-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.go
More file actions
287 lines (262 loc) · 6.77 KB
/
functions.go
File metadata and controls
287 lines (262 loc) · 6.77 KB
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 templater
import (
"fmt"
"github.com/malumar/domaintools"
"github.com/malumar/strutils"
"golang.org/x/net/html"
"html/template"
"log"
"reflect"
"regexp"
"strings"
"time"
)
// Zamiana " Text More here "
// na "Text More here"
func ReduceWhiteSpaces(input string) string {
final := re_leadclose_whtsp.ReplaceAllString(input, "")
final = re_inside_whtsp.ReplaceAllString(final, " ")
return final
}
// liczba monga
func Plural(s string) string {
if len(s) == 0 {
return ""
}
if s[len(s)-1:] == "s" {
return s + "es"
} else {
return s + "s"
}
}
func IsEmpty(v interface{}) bool {
if v == nil || v == "" || v == false {
return true
}
tv := reflect.TypeOf(v)
vv := reflect.ValueOf(v)
switch tv.Kind() {
case reflect.Map, reflect.Slice:
return vv.Len() == 0
case reflect.String:
return vv.String() == ""
case reflect.Int:
return vv.Int() == 0
case reflect.Uint:
return vv.Uint() == 0
case reflect.Float32, reflect.Float64:
return vv.Float() == 0
case reflect.Bool:
return vv.Bool() == false
break
}
return false
}
func IsNotEmpty(v interface{}) bool {
return !IsEmpty(v)
}
func FirstLower(s string) string {
if len(s) == 0 {
return ""
}
return strings.ToLower(s[:1]) + s[1:]
}
func FirstUpper(s string) string {
if len(s) == 0 {
return ""
}
return strings.ToUpper(s[:1]) + s[1:]
}
func IsLast(x int, a interface{}) bool {
return x == reflect.ValueOf(a).Len()-1
}
func init() {
functions = map[string]interface{}{
"set_html": func(renderArgs map[string]interface{}, key string, value interface{}) template.JS {
renderArgs[key] = value
return template.JS("")
},
"append_html": func(renderArgs map[string]interface{}, key string, value interface{}) template.JS {
if renderArgs[key] == nil {
renderArgs[key] = []interface{}{value}
} else {
renderArgs[key] = append(renderArgs[key].([]interface{}), value)
}
return template.JS("")
},
"firstOf": func(args ...interface{}) interface{} {
for _, val := range args {
switch val.(type) {
case nil:
continue
case string:
if val == "" {
continue
}
return val
default:
return val
}
}
return nil
},
"printIfNotLast": func(index int, len int, what string) string {
if index+1 < len {
return fmt.Sprintf("%d z %d", index+1, len)
}
return ""
},
// Pads the given string with space up to the given width.
"pad_html": func(str string, width int) template.HTML {
if len(str) >= width {
return template.HTML(html.EscapeString(str))
}
return template.HTML(html.EscapeString(str) + strings.Repeat(" ", width-len(str)))
},
// Pads the given string with space up to the given width.
"pad": func(str string, width int) string {
if len(str) >= width {
return str
}
return str + strings.Repeat(" ", width-len(str))
},
// Pluralize, a helper for pluralizing words to correspond to data of dynamic length.
// items - a slice of items, or an integer indicating how many items there are.
// pluralOverrides - optional arguments specifying the output in the
// singular and plural cases. by default "" and "s"
"pluralize": func(items interface{}, pluralOverrides ...string) string {
singular, plural := "", "s"
if len(pluralOverrides) >= 1 {
singular = pluralOverrides[0]
if len(pluralOverrides) == 2 {
plural = pluralOverrides[1]
}
}
switch v := reflect.ValueOf(items); v.Kind() {
case reflect.Int:
if items.(int) != 1 {
return plural
}
case reflect.Slice:
if v.Len() != 1 {
return plural
}
default:
log.Println("pluralize: unexpected type: ", v)
}
return singular
},
// 2006-01-02 15:04:05
"year": func(date time.Time) string {
return date.Format("2006")
},
"yearZero": func(date time.Time) string {
return date.Format("06")
},
"monthZero": func(date time.Time) string {
return date.Format("01")
},
"dayZero": func(date time.Time) string {
return date.Format("02")
},
"hourZero": func(date time.Time) string {
return date.Format("15")
},
"minuteZero": func(date time.Time) string {
return date.Format("04")
},
"secZero": func(date time.Time) string {
return date.Format("05")
},
"even": func(a int) bool { return (a % 2) == 0 },
// zwiększenie licznika
"inc": func(v int) int {
return v + 1
},
// zmniejszenie licznika
"dec": func(v int) int {
return v - 1
},
// liczba monga dla słów angielskich
"plural_en": Plural,
// Tekst -> tekst
"firstLower": FirstLower,
"firstUpper": FirstUpper,
// Tekst -> t
"firstCharLower": func(s string) string {
if s == "" {
return ""
}
return strings.ToLower(s[:1])
},
// tekst -> T
"firstCharUpper": func(s string) string {
if s == "" {
return ""
}
return strings.ToLower(s[:1])
},
"bool2str": func(val bool, ifTrue, ifFalse string) string {
if val {
return ifTrue
} else {
return ifFalse
}
},
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"trimSpace": strings.TrimSpace,
"replaceNewLineToSpace": func(s string) string {
return strings.Replace(s, "\n", " ", -1)
},
"removeWhiteSpace": func(s string) string {
return strings.Replace(strings.Replace(strings.Replace(s, "\n", "", -1), " ", "", -1), " ", "", -1)
},
// Znak `
"esc": func() string {
return "`"
},
"replaceString": func(old string, new string, searchIn string) string {
return strings.Replace(searchIn, old, new, -1)
},
"isNotLast": func(x int, a interface{}) bool {
return !IsLast(x, a)
},
// czy wartość jest pusta
"isEmpty": IsEmpty,
// czy wartość jest pusta
"isNotEmpty": IsNotEmpty,
// zmienna|islast $x czy item $x jest ju ostatni
"isLast": IsLast,
"concat": func(sep string, args ...string) string {
return strings.Join(args, sep)
},
// zmienna|addsfx "_sfx" --> zmienna_sfx
"addSfx": func(sfx string, s string) string {
return s + sfx
},
// zmienna|addsfx "pfx_" --> pfx_zmienna
"addPfx": func(pfx string, s string) string {
return pfx + s
},
// zamiana:
// zmienna -> zmienna
// zmiennaInna -> zmienna_Inna
// ZmiennnaInnaDruga -> Zmienna_Inna_Drug
"underScoreCase": strutils.UnderscoreCase,
"flatCase": strutils.FlatCase,
// zamiana:
// zmienna -> ZMIENNA
// zmiennaInna -> ZMIENNA_INNA
// ZmiennnaInnaDruga -> ZMIENNA_INNA_DRUGA
"underScoreCaseUpper": strutils.UnderscoreCaseUpper,
"snakeCaseFirstLower": strutils.SnakeCaseFirstLower,
"safeHTML": func(s string) template.HTML {
return template.HTML(s)
},
"toSafeAsciiDomainName": domaintools.SafeAsciiDomainName,
"reduceWhiteSpaces": ReduceWhiteSpaces,
}
}
var re_leadclose_whtsp *regexp.Regexp = regexp.MustCompile(`^[\s\p{Zs}]+|[\s\p{Zs}]+$`)
var re_inside_whtsp *regexp.Regexp = regexp.MustCompile(`[\s\p{Zs}]{2,}`)