-
Notifications
You must be signed in to change notification settings - Fork 170
/
templatehelper.go
83 lines (78 loc) Β· 2 KB
/
templatehelper.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
package ansi
import (
"regexp"
"strings"
"text/template"
)
// TemplateFuncMap contains a few useful template helpers
var (
TemplateFuncMap = template.FuncMap{
"Left": func(values ...interface{}) string {
s := values[0].(string)
n := values[1].(int)
if n > len(s) {
n = len(s)
}
return s[:n]
},
"Matches": func(values ...interface{}) bool {
ok, _ := regexp.MatchString(values[1].(string), values[0].(string))
return ok
},
"Mid": func(values ...interface{}) string {
s := values[0].(string)
l := values[1].(int)
if l > len(s) {
l = len(s)
}
if len(values) > 2 {
r := values[2].(int)
if r > len(s) {
r = len(s)
}
return s[l:r]
}
return s[l:]
},
"Right": func(values ...interface{}) string {
s := values[0].(string)
n := len(s) - values[1].(int)
if n < 0 {
n = 0
}
return s[n:]
},
"Last": func(values ...interface{}) string {
return values[0].([]string)[len(values[0].([]string))-1]
},
// strings functions
"Compare": strings.Compare, // 1.5+ only
"Contains": strings.Contains,
"ContainsAny": strings.ContainsAny,
"Count": strings.Count,
"EqualFold": strings.EqualFold,
"HasPrefix": strings.HasPrefix,
"HasSuffix": strings.HasSuffix,
"Index": strings.Index,
"IndexAny": strings.IndexAny,
"Join": strings.Join,
"LastIndex": strings.LastIndex,
"LastIndexAny": strings.LastIndexAny,
"Repeat": strings.Repeat,
"Replace": strings.Replace,
"Split": strings.Split,
"SplitAfter": strings.SplitAfter,
"SplitAfterN": strings.SplitAfterN,
"SplitN": strings.SplitN,
"Title": strings.Title,
"ToLower": strings.ToLower,
"ToTitle": strings.ToTitle,
"ToUpper": strings.ToUpper,
"Trim": strings.Trim,
"TrimLeft": strings.TrimLeft,
"TrimPrefix": strings.TrimPrefix,
"TrimRight": strings.TrimRight,
"TrimSpace": strings.TrimSpace,
"TrimSuffix": strings.TrimSuffix,
}
)