Skip to content

Commit 4cabc70

Browse files
committed
Add ToHeaderField
1 parent 17a82ee commit 4cabc70

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
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

+40-11
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,23 +202,40 @@ 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 {
196209
t.Run(tt.input, func(t *testing.T) {
197-
require.Equal(t, tt.pascal, ToPascalCase(tt.input))
198-
require.Equal(t, tt.camel, ToCamelCase(tt.input))
199-
require.Equal(t, tt.snake, ToSnakeCase(tt.input))
210+
t.Run("ToPascalCase", func(t *testing.T) {
211+
require.Equal(t, tt.pascal, ToPascalCase(tt.input))
212+
})
200213

201-
if tt.pascalGo == "" {
202-
tt.pascalGo = tt.pascal
203-
}
204-
require.Equal(t, tt.pascalGo, ToPascalGoCase(tt.input))
214+
t.Run("ToCamelCase", func(t *testing.T) {
215+
require.Equal(t, tt.camel, ToCamelCase(tt.input))
216+
})
205217

206-
if tt.camelGo == "" {
207-
tt.camelGo = tt.camel
208-
}
209-
require.Equal(t, tt.camelGo, ToCamelGoCase(tt.input))
218+
t.Run("ToSnakeCase", func(t *testing.T) {
219+
require.Equal(t, tt.snake, ToSnakeCase(tt.input))
220+
})
221+
222+
t.Run("ToPascalGoCase", func(t *testing.T) {
223+
if tt.pascalGo == "" {
224+
tt.pascalGo = tt.pascal
225+
}
226+
require.Equal(t, tt.pascalGo, ToPascalGoCase(tt.input))
227+
})
228+
229+
t.Run("ToCamelGoCase", func(t *testing.T) {
230+
if tt.camelGo == "" {
231+
tt.camelGo = tt.camel
232+
}
233+
require.Equal(t, tt.camelGo, ToCamelGoCase(tt.input))
234+
})
235+
236+
t.Run("ToHeaderField", func(t *testing.T) {
237+
require.Equal(t, tt.header, ToHeaderField(tt.input))
238+
})
210239
})
211240
}
212241
}

0 commit comments

Comments
 (0)