Skip to content

Commit e226443

Browse files
committed
added additional 2 conversion settings : first symbol case and the whole text case
1 parent 2937b7e commit e226443

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

stringstyle_formatter.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ import (
66
)
77

88
type FormattingStyle string
9+
type CaseSetting int
910

1011
const (
1112
Camel FormattingStyle = "camel"
1213
Snake FormattingStyle = "snake"
1314
Kebab FormattingStyle = "kebab"
1415
)
1516

17+
const (
18+
ToUpper CaseSetting = 1
19+
ToLower CaseSetting = 2
20+
NoChanges CaseSetting = 3
21+
)
22+
1623
type styleInc struct {
1724
Index int
1825
Style FormattingStyle
@@ -23,7 +30,22 @@ var styles = map[rune]FormattingStyle{
2330
'-': Kebab,
2431
}
2532

26-
func SetStyle(text *string, style FormattingStyle) string {
33+
// SetStyle is a function that converts text with code to defined code style.
34+
/* Set text like a code style to on from FormattingStyle (Camel, Snake, or Kebab)
35+
* conversion of abbreviations like JSON, USB, and so on is going like a regular text
36+
* for current version, therefore they these abbreviations could be in a different
37+
* case after conversion.
38+
* Case settings apply in the following order : 1 - textCase, 2 - firstSymbol.
39+
* If you are not applying textCase to text converting from Camel to Snake or Kebab
40+
* result is lower case styled text. textCase does not apply to Camel style.
41+
* Parameters:
42+
* - text - pointer to text
43+
* - style - new code style
44+
* - firstSymbol - case settings for first symbol
45+
* - textCase - case settings for whole text except first symbol
46+
* Returns : new string with formatted line
47+
*/
48+
func SetStyle(text *string, style FormattingStyle, firstSymbol CaseSetting, textCase CaseSetting) string {
2749
if text == nil {
2850
return ""
2951
}
@@ -65,10 +87,32 @@ func SetStyle(text *string, style FormattingStyle) string {
6587
}
6688
}
6789
sb.WriteString((*text)[startIndex:])
90+
result := strings.Builder{}
6891
if style != Camel {
69-
return strings.ToLower(sb.String())
92+
switch textCase {
93+
case ToUpper:
94+
result.WriteString(strings.ToUpper(sb.String()[1:]))
95+
break
96+
case ToLower:
97+
result.WriteString(strings.ToLower(sb.String()[1:]))
98+
break
99+
case NoChanges:
100+
result.WriteString(sb.String()[1:])
101+
break
102+
}
103+
} else {
104+
result.WriteString(sb.String()[1:])
105+
}
106+
107+
switch firstSymbol {
108+
case ToUpper:
109+
return strings.ToUpper(sb.String()[:1]) + result.String()
110+
case ToLower:
111+
return strings.ToLower(sb.String()[:1]) + result.String()
112+
case NoChanges:
113+
return sb.String()[:1] + result.String()
70114
}
71-
return sb.String()
115+
return ""
72116
}
73117

74118
// defineFormattingStyle

stringstyle_formatter_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77
)
88

9-
func TestSetFormattingStyle(t *testing.T) {
9+
func TestSetFormattingStyleWithoutCaseModification(t *testing.T) {
1010
for name, test := range map[string]struct {
1111
text string
1212
expected string
@@ -24,7 +24,7 @@ func TestSetFormattingStyle(t *testing.T) {
2424
},
2525
"lower-case-camel-to-snake-simple": {
2626
text: "mySuperFunc",
27-
expected: "my_super_func",
27+
expected: "my_Super_Func",
2828
newStyle: stringFormatter.Snake,
2929
},
3030
"snake-to-camel-simple": {
@@ -34,7 +34,7 @@ func TestSetFormattingStyle(t *testing.T) {
3434
},
3535
"camel-to-snake-with-underscore-the-end": {
3636
text: "myVal_",
37-
expected: "my_val_",
37+
expected: "my_Val_",
3838
newStyle: stringFormatter.Snake,
3939
},
4040
"mixed-to-camel-simple": {
@@ -54,12 +54,13 @@ func TestSetFormattingStyle(t *testing.T) {
5454
},
5555
"camel-with_abbreviation-to-snake": {
5656
text: "convertToJSON",
57-
expected: "convert_to_json",
57+
expected: "convert_To_JSON",
5858
newStyle: stringFormatter.Snake,
5959
},
6060
} {
6161
t.Run(name, func(t *testing.T) {
62-
actual := stringFormatter.SetStyle(&test.text, test.newStyle)
62+
actual := stringFormatter.SetStyle(&test.text, test.newStyle, stringFormatter.NoChanges,
63+
stringFormatter.NoChanges)
6364
assert.Equal(t, test.expected, actual)
6465
})
6566
}

0 commit comments

Comments
 (0)