Skip to content

Commit b49cf25

Browse files
committed
Add ToHeaderField
1 parent 17a82ee commit b49cf25

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

strcase/id.go

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ func ToSnakeCase(input string) string {
4141
return SplitJoin(input, CaseStrategyNever, '_', false)
4242
}
4343

44+
// ToHeaderField transforms a string in any form to An-HTTP-Header.
45+
func ToHeaderField(input string) string {
46+
return SplitJoin(input, CaseStrategyTitle, '-', true)
47+
}
48+
4449
func SplitJoin(input string, caseStrategy CaseStrategy, separator rune, initialism bool) string {
4550
firstUpper := int(caseStrategy)
4651
b := allocateBuilder(input, separator)

strcase/id_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func Test_splitJoin(t *testing.T) {
106106
pascal string
107107
pascalGo string
108108
snake string
109+
header string
109110
}{
110111
{
111112
// everything empty
@@ -115,30 +116,35 @@ func Test_splitJoin(t *testing.T) {
115116
pascal: "A",
116117
camel: "a",
117118
snake: "a",
119+
header: "A",
118120
},
119121
{
120122
input: "A",
121123
pascal: "A",
122124
camel: "a",
123125
snake: "a",
126+
header: "A",
124127
},
125128
{
126129
input: "a_a",
127130
pascal: "AA",
128131
camel: "aA",
129132
snake: "a_a",
133+
header: "A-A",
130134
},
131135
{
132136
input: "__a___a_",
133137
pascal: "AA",
134138
camel: "aA",
135139
snake: "a_a",
140+
header: "A-A",
136141
},
137142
{
138143
input: "aa_bbb",
139144
pascal: "AaBbb",
140145
camel: "aaBbb",
141146
snake: "aa_bbb",
147+
header: "Aa-Bbb",
142148
},
143149
{
144150
input: "aa_id",
@@ -147,18 +153,21 @@ func Test_splitJoin(t *testing.T) {
147153
camel: "aaId",
148154
camelGo: "aaID",
149155
snake: "aa_id",
156+
header: "Aa-ID",
150157
},
151158
{
152159
input: "fooBar",
153160
pascal: "FooBar",
154161
camel: "fooBar",
155162
snake: "foo_bar",
163+
header: "Foo-Bar",
156164
},
157165
{
158166
input: "FooBAR",
159167
pascal: "FooBar",
160168
camel: "fooBar",
161169
snake: "foo_bar",
170+
header: "Foo-Bar",
162171
},
163172
{
164173
input: "fooUrl",
@@ -167,6 +176,7 @@ func Test_splitJoin(t *testing.T) {
167176
camel: "fooUrl",
168177
camelGo: "fooURL",
169178
snake: "foo_url",
179+
header: "Foo-URL",
170180
},
171181
{
172182
input: "fooURL",
@@ -175,13 +185,15 @@ func Test_splitJoin(t *testing.T) {
175185
camel: "fooUrl",
176186
camelGo: "fooURL",
177187
snake: "foo_url",
188+
header: "Foo-URL",
178189
},
179190
{
180191
input: "url10",
181192
pascal: "Url10",
182193
pascalGo: "URL10",
183194
camel: "url10",
184195
snake: "url_10",
196+
header: "URL-10",
185197
},
186198
{
187199
input: "url_id",
@@ -190,6 +202,7 @@ func Test_splitJoin(t *testing.T) {
190202
camel: "urlId",
191203
camelGo: "urlID",
192204
snake: "url_id",
205+
header: "URL-ID",
193206
},
194207
}
195208
for _, tt := range tests {

0 commit comments

Comments
 (0)