-
Notifications
You must be signed in to change notification settings - Fork 32
/
strings.go
336 lines (254 loc) · 6.15 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package processors
import (
"fmt"
"github.com/abhimanyu003/sttr/utils"
"regexp"
"strings"
"github.com/iancoleman/strcase"
)
// Lower converts a string to lower case.
// Example: "THIS IS STRING" to "this is string".
type Lower struct{}
func (p Lower) Name() string {
return "lower"
}
func (p Lower) Alias() []string {
return nil
}
func (p Lower) Transform(data []byte, _ ...Flag) (string, error) {
return strings.ToLower(string(data)), nil
}
func (p Lower) Flags() []Flag {
return nil
}
func (p Lower) Title() string {
return "To Lower case"
}
func (p Lower) Description() string {
return "Transform your text to lower case"
}
func (p Lower) FilterValue() string {
return p.Title()
}
// Upper convert string to upper case.
// Example: "this is string" to "THIS IS STRING".
type Upper struct{}
func (p Upper) Name() string {
return "upper"
}
func (p Upper) Alias() []string {
return nil
}
func (p Upper) Transform(data []byte, _ ...Flag) (string, error) {
return strings.ToUpper(string(data)), nil
}
func (p Upper) Flags() []Flag {
return nil
}
func (p Upper) Title() string {
return "To Upper case"
}
func (p Upper) Description() string {
return "Transform your text to UPPER CASE"
}
func (p Upper) FilterValue() string {
return p.Title()
}
// Title convert string to title case.
// Example: "this is string" to "This Is String".
type Title struct{}
func (p Title) Name() string {
return "title"
}
func (p Title) Alias() []string {
return nil
}
func (p Title) Transform(data []byte, _ ...Flag) (string, error) {
return strings.Title(string(data)), nil
}
func (p Title) Flags() []Flag {
return nil
}
func (p Title) Title() string {
return "To Title Case"
}
func (p Title) Description() string {
return "Transform your text to Title Case"
}
func (p Title) FilterValue() string {
return p.Title()
}
// Snake convert string to snake_case.
// Example: "this is string" to "this_is_string".
type Snake struct{}
func (p Snake) Name() string {
return "snake"
}
func (p Snake) Alias() []string {
return nil
}
func (p Snake) Transform(data []byte, _ ...Flag) (string, error) {
str := regexp.MustCompile(`\s+`).ReplaceAllString(string(data), " ")
return strcase.ToSnake(str), nil
}
func (p Snake) Flags() []Flag {
return nil
}
func (p Snake) Title() string {
return "To Snake case"
}
func (p Snake) Description() string {
return "Transform your text to snake_case"
}
func (p Snake) FilterValue() string {
return p.Title()
}
// Kebab convert string to kebab-case.
// Example: "this is string" to "this-is-string".
type Kebab struct{}
func (p Kebab) Name() string {
return "kebab"
}
func (p Kebab) Alias() []string {
return nil
}
func (p Kebab) Transform(data []byte, _ ...Flag) (string, error) {
return utils.ToKebabCase(data), nil
}
func (p Kebab) Flags() []Flag {
return nil
}
func (p Kebab) Title() string {
return "To Kebab case"
}
func (p Kebab) Description() string {
return "Transform your text to kebab-case"
}
func (p Kebab) FilterValue() string {
return p.Title()
}
// Camel convert string to CamelCase.
// Example: "this is string" to "ThisIsString".
type Camel struct{}
func (p Camel) Name() string {
return "camel"
}
func (p Camel) Alias() []string {
return nil
}
func (p Camel) Transform(data []byte, _ ...Flag) (string, error) {
str := regexp.MustCompile(`\s+`).ReplaceAllString(string(data), " ")
return strcase.ToCamel(str), nil
}
func (p Camel) Flags() []Flag {
return nil
}
func (p Camel) Title() string {
return "To Camel case"
}
func (p Camel) Description() string {
return "Transform your text to CamelCase"
}
func (p Camel) FilterValue() string {
return p.Title()
}
// Slug convert string to StringToSlug. It's similar to Kebab case but URL Friendly.
// Example: "this is string" to "this-is-string".
type Slug struct{}
func (p Slug) Name() string {
return "slug"
}
func (p Slug) Alias() []string {
return nil
}
func (p Slug) Transform(data []byte, _ ...Flag) (string, error) {
re := regexp.MustCompile("[^a-z0-9]+")
return strings.Trim(re.ReplaceAllString(strings.ToLower(string(data)), "-"), "-"), nil
}
func (p Slug) Flags() []Flag {
return nil
}
func (p Slug) Title() string {
return "To Slug case"
}
func (p Slug) Description() string {
return "Transform your text to slug-case"
}
func (p Slug) FilterValue() string {
return p.Title()
}
// CountCharacters count number of Characters including spaces.
type CountCharacters struct{}
func (p CountCharacters) Name() string {
return "count-chars"
}
func (p CountCharacters) Alias() []string {
return nil
}
func (p CountCharacters) Transform(data []byte, _ ...Flag) (string, error) {
return fmt.Sprintf("%d", len([]rune(string(data)))), nil
}
func (p CountCharacters) Flags() []Flag {
return nil
}
func (p CountCharacters) Title() string {
return "Count Number of Characters"
}
func (p CountCharacters) Description() string {
return "Find the length of your text (including spaces)"
}
func (p CountCharacters) FilterValue() string {
return p.Title()
}
// CountWords count number of words in string.
// Example: "hello world" = 2
type CountWords struct{}
func (p CountWords) Name() string {
return "count-words"
}
func (p CountWords) Alias() []string {
return nil
}
func (p CountWords) Transform(data []byte, _ ...Flag) (string, error) {
return fmt.Sprintf("%d", len(strings.Fields(string(data)))), nil
}
func (p CountWords) Flags() []Flag {
return nil
}
func (p CountWords) Title() string {
return "Count Number of Words"
}
func (p CountWords) Description() string {
return "Count the number of words in your text"
}
func (p CountWords) FilterValue() string {
return p.Title()
}
// Reverse reverse a given string
// Example: "test" to "tset"
type Reverse struct{}
func (p Reverse) Name() string {
return "reverse"
}
func (p Reverse) Alias() []string {
return nil
}
func (p Reverse) Transform(data []byte, _ ...Flag) (string, error) {
result := ""
for _, v := range data {
result = string(v) + result
}
return result, nil
}
func (p Reverse) Flags() []Flag {
return nil
}
func (p Reverse) Title() string {
return "Reverse text"
}
func (p Reverse) Description() string {
return "Reverse Text ( txeT esreveR )"
}
func (p Reverse) FilterValue() string {
return p.Title()
}