-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildin_string.go
190 lines (166 loc) · 6.47 KB
/
buildin_string.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
package template
import (
"fmt"
"log"
"github.com/kaptinlin/filter"
)
func init() {
// Register string-related filters
filtersToRegister := map[string]FilterFunc{
"default": defaultFilter,
"trim": trimFilter,
"split": splitFilter,
"replace": replaceFilter,
"remove": removeFilter,
"append": appendFilter,
"prepend": prependFilter,
"length": lengthFilter,
"upper": upperFilter,
"lower": lowerFilter,
"titleize": titleizeFilter,
"capitalize": capitalizeFilter,
"camelize": camelizeFilter,
"pascalize": pascalizeFilter,
"dasherize": dasherizeFilter,
"slugify": slugifyFilter,
"pluralize": pluralizeFilter,
"ordinalize": ordinalizeFilter,
"truncate": truncateFilter,
"truncateWords": truncateWordsFilter,
}
for name, filterFunc := range filtersToRegister {
if err := RegisterFilter(name, filterFunc); err != nil {
log.Printf("Error registering filter %s: %v", name, err)
}
}
}
// defaultFilter returns a default value if the input string is empty.
func defaultFilter(value interface{}, args ...string) (interface{}, error) {
defaultValue := ""
if len(args) > 0 {
defaultValue = args[0]
}
return filter.Default(toString(value), defaultValue), nil
}
// trimFilter removes leading and trailing whitespace from the string.
func trimFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Trim(toString(value)), nil
}
// splitFilter divides a string into a slice of strings based on a specified delimiter.
func splitFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: split filter requires a delimiter argument", ErrInsufficientArgs)
}
delimiter := args[0]
return filter.Split(toString(value), delimiter), nil
}
// replaceFilter substitutes all instances of a specified substring with another string.
func replaceFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 2 {
return nil, fmt.Errorf("%w: replace filter requires two arguments (old and new substrings)", ErrInsufficientArgs)
}
old, new := args[0], args[1]
return filter.Replace(toString(value), old, new), nil
}
// removeFilter eliminates all occurrences of a specified substring.
func removeFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: remove filter requires a substring argument", ErrInsufficientArgs)
}
toRemove := args[0]
return filter.Remove(toString(value), toRemove), nil
}
// appendFilter adds characters to the end of a string.
func appendFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: append filter requires a string to append", ErrInsufficientArgs)
}
toAppend := args[0]
return filter.Append(toString(value), toAppend), nil
}
// prependFilter adds characters to the beginning of a string.
func prependFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: prepend filter requires a string to prepend", ErrInsufficientArgs)
}
toPrepend := args[0]
return filter.Prepend(toString(value), toPrepend), nil
}
// lengthFilter returns the number of characters in a string.
func lengthFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Length(toString(value)), nil
}
// upperFilter converts all characters in a string to uppercase.
func upperFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Upper(toString(value)), nil
}
// lowerFilter converts all characters in a string to lowercase.
func lowerFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Lower(toString(value)), nil
}
// titleizeFilter capitalizes the first letter of each word in a string.
func titleizeFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Titleize(toString(value)), nil
}
// capitalizeFilter capitalizes the first letter of a string.
func capitalizeFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Capitalize(toString(value)), nil
}
// camelizeFilter converts a string to camelCase.
func camelizeFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Camelize(toString(value)), nil
}
// pascalizeFilter converts a string to PascalCase.
func pascalizeFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Pascalize(toString(value)), nil
}
// dasherizeFilter transforms a string into a lowercased, dash-separated format.
func dasherizeFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Dasherize(toString(value)), nil
}
// slugifyFilter converts a string into a URL-friendly "slug".
func slugifyFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Slugify(toString(value)), nil
}
// pluralizeFilter determines the singular or plural form of a word based on a numeric value.
func pluralizeFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 2 {
return nil, fmt.Errorf("%w: pluralize filter requires two arguments (singular and plural forms)", ErrInsufficientArgs)
}
count, err := toInteger(value)
if err != nil {
return nil, err
}
singular, plural := args[0], args[1]
return filter.Pluralize(count, singular, plural), nil
}
// ordinalizeFilter converts a number to its ordinal English form.
func ordinalizeFilter(value interface{}, args ...string) (interface{}, error) {
number, err := toInteger(value)
if err != nil {
return nil, err
}
return filter.Ordinalize(number), nil
}
// truncateFilter shortens a string to a specified length and appends "..." if it exceeds that length.
func truncateFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: truncate filter requires a length argument", ErrInsufficientArgs)
}
maxLength, err := toInteger(args[0])
if err != nil {
return nil, err
}
return filter.Truncate(toString(value), maxLength), nil
}
// truncateWordsFilter truncates a string to a specified number of words, appending "..." if it exceeds that limit.
func truncateWordsFilter(value interface{}, args ...string) (interface{}, error) {
if len(args) < 1 {
return nil, fmt.Errorf("%w: truncateWords filter requires a word count argument", ErrInsufficientArgs)
}
maxWords, err := toInteger(args[0])
if err != nil {
return nil, err
}
return filter.TruncateWords(toString(value), maxWords), nil
}