-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscs.go
422 lines (378 loc) · 10.3 KB
/
scs.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package scs
import (
"fmt"
"strings"
)
const (
// Camel is constant that characterizes string case style as camelCase.
Camel CaseStyle = 1 << iota
// Kebab is constant that characterizes string case style as kebab-case.
Kebab
// Pascal is constant that characterizes string case style as PascalCase.
Pascal
// Snake is constant that characterizes string case style as snake_case.
Snake
)
// CaseStyle is string case style type.
type CaseStyle uint8
// StringCaseStyle is object of the string case style (SCS).
// It can be created correctly through the New function only.
type StringCaseStyle struct {
do func(string) string // convert raw string to the case-styling value
style CaseStyle // is the flag of the string case style
value string // value in case-style format
isValid bool // true if the object was created correctly
}
// New returns a pointer to a string case style object. The style defines
// the string case style. a string (or list of strings) to format.
//
// This function creates a new instance of the StringCaseStyle struct, which
// represents a specific string case style. It takes a CaseStyle parameter
// to determine the desired case style (Camel, Kebab, Pascal, Snake), and
// one or more string values to be formatted.
//
// The function initializes the `do` field of the StringCaseStyle struct with
// the appropriate formatting function based on the specified case style.
// It then joins the input strings with a space separator and applies the
// formatting function to the resulting string. The formatted string is stored
// in the `value` field of the StringCaseStyle struct.
//
// If an incorrect case style is provided, the function returns an error along
// with a nil pointer to the StringCaseStyle.
//
// Example usage:
//
// style, err := scs.New(scs.Camel, "hello", "world")
func New(style CaseStyle, value ...string) (*StringCaseStyle, error) {
var do func(string) string
switch style {
case Camel:
do = StrToCamel
case Kebab:
do = StrToKebab
case Pascal:
do = StrToPascal
case Snake:
do = StrToSnake
default:
return &StringCaseStyle{do: func(s string) string { return s }},
fmt.Errorf("incorrect case style")
}
return &StringCaseStyle{
do: do,
style: style,
value: do(strings.Join(value, " ")),
isValid: true,
}, nil
}
// IsValid returns true if the StringCaseStyle object is valid.
//
// This method is used to check the validity of a StringCaseStyle object.
// It returns true if the object is valid, indicating that the string case
// style and value have been set successfully.
//
// Example usage:
//
// style, _ := New(Camel, "helloWorld")
// isValid := style.IsValid()
// // isValid: true
//
// invalidStyle := &StringCaseStyle{isValid: false}
// isValid := invalidStyle.IsValid()
// // isValid: false
func (o *StringCaseStyle) IsValid() bool {
return o.isValid
}
// IsCamel returns true if the StringCaseStyle object represents
// a camelCase value.
//
// This method checks if the StringCaseStyle object has a camelCase style.
// It returns true if the object represents a camelCase value, indicating
// that the original value or formatted value is in camelCase.
//
// Example usage:
//
// style, _ := New(Camel, "helloWorld")
// isCamel := style.IsCamel()
// // isCamel: true
//
// style, _ = New(Pascal, "HelloWorld")
// isCamel = style.IsCamel()
// // isCamel: false
func (o *StringCaseStyle) IsCamel() bool {
return o.style == Camel
}
// IsKebab returns true if the StringCaseStyle object represents
// a kebab-case value.
//
// This method checks if the StringCaseStyle object has a kebab-case style.
// It returns true if the object represents a kebab-case value, indicating
// that the original value or formatted value is in kebab-case.
//
// Example usage:
//
// style, _ := New(Kebab, "hello-world")
// isKebab := style.IsKebab()
// // isKebab: true
//
// style, _ = New(Pascal, "HelloWorld")
// isKebab = style.IsKebab()
// // isKebab: false
func (o *StringCaseStyle) IsKebab() bool {
return o.style == Kebab
}
// IsPascal returns true if the StringCaseStyle object represents
// a PascalCase value.
//
// This method checks if the StringCaseStyle object has a PascalCase style.
// It returns true if the object represents a PascalCase value, indicating
// that the original value or formatted value is in PascalCase.
//
// Example usage:
//
// style, _ := New(Pascal, "HelloWorld")
// isPascal := style.IsPascal()
// // isPascal: true
//
// style, _ = New(Snake, "hello_world")
// isPascal = style.IsPascal()
// // isPascal: false
func (o *StringCaseStyle) IsPascal() bool {
return o.style == Pascal
}
// IsSnake returns true if the StringCaseStyle object represents
// a snake_case value.
//
// This method checks if the StringCaseStyle object has a snake_case style.
// It returns true if the object represents a snake_case value, indicating
// that the original value or formatted value is in snake_case.
//
// Example usage:
//
// style, _ := New(Snake, "hello_world")
// isSnake := style.IsSnake()
// // isSnake: true
//
// style, _ = New(Pascal, "HelloWorld")
// isSnake = style.IsSnake()
// // isSnake: false
func (o *StringCaseStyle) IsSnake() bool {
return o.style == Snake
}
// Eat converts a string to the specified style and stores it
// as the object value.
//
// This method converts the input string to the style defined by the
// StringCaseStyle object and updates the object's value.
// The converted value is returned as the result.
//
// Example usage:
//
// style, _ := New(Camel, "hello_world")
// result := style.Eat("example_input")
// // result: "exampleInput"
// // style.Value(): "exampleInput"
//
// style, _ = New(Kebab, "hello-world")
// result = style.Eat("example_input")
// // result: "example-input"
// // style.Value(): "example-input"
func (o *StringCaseStyle) Eat(s string) string {
o.value = o.do(s)
return o.value
}
// Set sets a new value for the StringCaseStyle object.
//
// This method converts the input string to the style defined by the
// StringCaseStyle object and sets the converted value as the new value
// of the object. The updated object is returned for method chaining.
//
// Example usage:
//
// style, _ := New(Camel, "hello_world")
// result := style.Set("example_input")
// // result.Value(): "exampleInput"
//
// style, _ = New(Kebab, "hello-world")
// result = style.Set("example_input")
// // result.value: "example-input"
func (o *StringCaseStyle) Set(s string) *StringCaseStyle {
o.value = o.do(s)
return o
}
// Value returns the current value of the StringCaseStyle object.
//
// This method returns the current value of the StringCaseStyle object.
//
// Example usage:
//
// style, _ := New(Camel, "Hello World")
// result := style.Value()
// // result: "Hello World"
//
// style, _ = New(Kebab, "Show me")
// result = style.Value()
// // result: "Show me"
func (o *StringCaseStyle) Value() string {
return o.value
}
// CopyToCamel converts an object to Camel Type StringCaseStyle
// and returns new pointer to it.
func (o *StringCaseStyle) CopyToCamel() (*StringCaseStyle, error) {
var (
value string
err error
)
switch o.style {
case Camel:
value = o.value
case Kebab:
value, err = KebabToCamel(o.value)
case Pascal:
value, err = PascalToCamel(o.value)
case Snake:
value, err = SnakeToCamel(o.value)
}
return &StringCaseStyle{
do: StrToCamel,
style: Camel,
value: value,
isValid: err == nil,
}, err
}
// ToCamel converts an object to Camel Type StringCaseStyle.
func (o *StringCaseStyle) ToCamel() error {
obj, err := o.CopyToCamel()
o.style = obj.style
o.value = obj.value
o.do = obj.do
o.isValid = obj.isValid
return err
}
// CopyToKebab converts an object to Kebab Type StringCaseStyle
// and returns new pointer to it.
func (o *StringCaseStyle) CopyToKebab() (*StringCaseStyle, error) {
var (
value string
err error
)
switch o.style {
case Camel:
value, err = CamelToKebab(o.value)
case Kebab:
value = o.value
case Pascal:
value, err = PascalToKebab(o.value)
case Snake:
value, err = SnakeToKebab(o.value)
}
return &StringCaseStyle{
do: StrToKebab,
style: Kebab,
value: value,
isValid: err == nil,
}, err
}
// ToKebab converts an object to Kebab Type StringCaseStyle.
func (o *StringCaseStyle) ToKebab() error {
if o.style == Kebab {
return nil
}
var newValue string
var err error
switch o.style {
case Camel:
newValue, err = CamelToKebab(o.value)
case Pascal:
newValue, err = PascalToKebab(o.value)
case Snake:
newValue, err = SnakeToKebab(o.value)
}
if err != nil {
return err
}
o.value = newValue
o.style = Kebab
o.do = StrToKebab
return nil
}
// CopyToPascal converts an object to Pascal Type StringCaseStyle
// and returns new pointer to it.
func (o *StringCaseStyle) CopyToPascal() (*StringCaseStyle, error) {
var (
value string
err error
)
switch o.style {
case Camel:
value, err = CamelToPascal(o.value)
case Kebab:
value, err = KebabToPascal(o.value)
case Pascal:
value = o.value
case Snake:
value, err = SnakeToPascal(o.value)
}
return &StringCaseStyle{
do: StrToPascal,
style: Pascal,
value: value,
isValid: err == nil,
}, err
}
// ToPascal converts an object to Pascal Type StringCaseStyle.
func (o *StringCaseStyle) ToPascal() error {
if o.style == Pascal {
return nil
}
var newValue string
var err error
switch o.style {
case Camel:
newValue, err = CamelToPascal(o.value)
case Kebab:
newValue, err = KebabToPascal(o.value)
case Snake:
newValue, err = SnakeToPascal(o.value)
}
if err != nil {
return err
}
o.value = newValue
o.style = Pascal
o.do = StrToPascal
return nil
}
// CopyToSnake converts an object to Snake Type StringCaseStyle
// and returns new pointer to it.
func (o *StringCaseStyle) CopyToSnake() (*StringCaseStyle, error) {
var (
value string
err error
)
switch o.style {
case Camel:
value, err = CamelToSnake(o.value)
case Kebab:
value, err = KebabToSnake(o.value)
case Pascal:
value, err = PascalToSnake(o.value)
case Snake:
value = o.value
}
return &StringCaseStyle{
do: StrToSnake,
style: Snake,
value: value,
isValid: err == nil,
}, err
}
// ToSnake converts an object to Snake Type StringCaseStyle.
func (o *StringCaseStyle) ToSnake() error {
obj, err := o.CopyToSnake()
o.style = obj.style
o.value = obj.value
o.do = obj.do
o.isValid = obj.isValid
return err
}