forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
210 lines (180 loc) · 4.94 KB
/
strings.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 funcs
// Namespace strings contains mostly wrappers of equivalently-named
// functions in the standard library `strings` package, with
// differences in argument order where it makes pipelining
// in templates easier.
import (
"sync"
"github.com/Masterminds/goutils"
"github.com/hairyhenderson/gomplate/conv"
"github.com/pkg/errors"
"strings"
"github.com/gosimple/slug"
gompstrings "github.com/hairyhenderson/gomplate/strings"
)
var (
strNS *StringFuncs
strNSInit sync.Once
)
// StrNS -
func StrNS() *StringFuncs {
strNSInit.Do(func() { strNS = &StringFuncs{} })
return strNS
}
// AddStringFuncs -
func AddStringFuncs(f map[string]interface{}) {
f["strings"] = StrNS
f["replaceAll"] = StrNS().ReplaceAll
f["title"] = StrNS().Title
f["toUpper"] = StrNS().ToUpper
f["toLower"] = StrNS().ToLower
f["trimSpace"] = StrNS().TrimSpace
f["indent"] = StrNS().Indent
f["sort"] = StrNS().Sort
// these are legacy aliases with non-pipelinable arg order
f["contains"] = strings.Contains
f["hasPrefix"] = strings.HasPrefix
f["hasSuffix"] = strings.HasSuffix
f["split"] = strings.Split
f["splitN"] = strings.SplitN
f["trim"] = strings.Trim
}
// StringFuncs -
type StringFuncs struct{}
// Abbrev -
func (f *StringFuncs) Abbrev(args ...interface{}) (string, error) {
str := ""
offset := 0
maxWidth := 0
if len(args) < 2 {
return "", errors.Errorf("abbrev requires a 'maxWidth' and 'input' argument")
}
if len(args) == 2 {
maxWidth = conv.ToInt(args[0])
str = conv.ToString(args[1])
}
if len(args) == 3 {
offset = conv.ToInt(args[0])
maxWidth = conv.ToInt(args[1])
str = conv.ToString(args[2])
}
if len(str) <= maxWidth {
return str, nil
}
return goutils.AbbreviateFull(str, offset, maxWidth)
}
// ReplaceAll -
func (f *StringFuncs) ReplaceAll(old, new string, s interface{}) string {
return strings.Replace(conv.ToString(s), old, new, -1)
}
// Contains -
func (f *StringFuncs) Contains(substr string, s interface{}) bool {
return strings.Contains(conv.ToString(s), substr)
}
// HasPrefix -
func (f *StringFuncs) HasPrefix(prefix string, s interface{}) bool {
return strings.HasPrefix(conv.ToString(s), prefix)
}
// HasSuffix -
func (f *StringFuncs) HasSuffix(suffix string, s interface{}) bool {
return strings.HasSuffix(conv.ToString(s), suffix)
}
// Repeat -
func (f *StringFuncs) Repeat(count int, s interface{}) (string, error) {
if count < 0 {
return "", errors.Errorf("negative count %d", count)
}
str := conv.ToString(s)
if count > 0 && len(str)*count/count != len(str) {
return "", errors.Errorf("count %d too long: causes overflow", count)
}
return strings.Repeat(str, count), nil
}
// Sort -
func (f *StringFuncs) Sort(list interface{}) ([]string, error) {
switch v := list.(type) {
case []string:
return gompstrings.Sort(v), nil
case []interface{}:
l := len(v)
b := make([]string, len(v))
for i := 0; i < l; i++ {
b[i] = conv.ToString(v[i])
}
return gompstrings.Sort(b), nil
default:
return nil, errors.Errorf("wrong type for value; expected []string; got %T", list)
}
}
// Split -
func (f *StringFuncs) Split(sep string, s interface{}) []string {
return strings.Split(conv.ToString(s), sep)
}
// SplitN -
func (f *StringFuncs) SplitN(sep string, n int, s interface{}) []string {
return strings.SplitN(conv.ToString(s), sep, n)
}
// Trim -
func (f *StringFuncs) Trim(cutset string, s interface{}) string {
return strings.Trim(conv.ToString(s), cutset)
}
// TrimPrefix -
func (f *StringFuncs) TrimPrefix(cutset string, s interface{}) string {
return strings.TrimPrefix(conv.ToString(s), cutset)
}
// TrimSuffix -
func (f *StringFuncs) TrimSuffix(cutset string, s interface{}) string {
return strings.TrimSuffix(conv.ToString(s), cutset)
}
// Title -
func (f *StringFuncs) Title(s interface{}) string {
return strings.Title(conv.ToString(s))
}
// ToUpper -
func (f *StringFuncs) ToUpper(s interface{}) string {
return strings.ToUpper(conv.ToString(s))
}
// ToLower -
func (f *StringFuncs) ToLower(s interface{}) string {
return strings.ToLower(conv.ToString(s))
}
// TrimSpace -
func (f *StringFuncs) TrimSpace(s interface{}) string {
return strings.TrimSpace(conv.ToString(s))
}
// Trunc -
func (f *StringFuncs) Trunc(length int, s interface{}) string {
return gompstrings.Trunc(length, conv.ToString(s))
}
// Indent -
func (f *StringFuncs) Indent(args ...interface{}) (string, error) {
input := conv.ToString(args[len(args)-1])
indent := " "
width := 1
var ok bool
switch len(args) {
case 2:
indent, ok = args[0].(string)
if !ok {
width, ok = args[0].(int)
if !ok {
return "", errors.New("Indent: invalid arguments")
}
indent = " "
}
case 3:
width, ok = args[0].(int)
if !ok {
return "", errors.New("Indent: invalid arguments")
}
indent, ok = args[1].(string)
if !ok {
return "", errors.New("Indent: invalid arguments")
}
}
return gompstrings.Indent(width, indent, input), nil
}
// Slug -
func (f *StringFuncs) Slug(in interface{}) string {
return slug.Make(conv.ToString(in))
}