Skip to content

Commit

Permalink
Add ToHeaderField
Browse files Browse the repository at this point in the history
  • Loading branch information
lavoiesl committed Aug 27, 2021
1 parent 17a82ee commit b49cf25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions strcase/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func ToSnakeCase(input string) string {
return SplitJoin(input, CaseStrategyNever, '_', false)
}

// ToHeaderField transforms a string in any form to An-HTTP-Header.
func ToHeaderField(input string) string {
return SplitJoin(input, CaseStrategyTitle, '-', true)
}

func SplitJoin(input string, caseStrategy CaseStrategy, separator rune, initialism bool) string {
firstUpper := int(caseStrategy)
b := allocateBuilder(input, separator)
Expand Down
13 changes: 13 additions & 0 deletions strcase/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func Test_splitJoin(t *testing.T) {
pascal string
pascalGo string
snake string
header string
}{
{
// everything empty
Expand All @@ -115,30 +116,35 @@ func Test_splitJoin(t *testing.T) {
pascal: "A",
camel: "a",
snake: "a",
header: "A",
},
{
input: "A",
pascal: "A",
camel: "a",
snake: "a",
header: "A",
},
{
input: "a_a",
pascal: "AA",
camel: "aA",
snake: "a_a",
header: "A-A",
},
{
input: "__a___a_",
pascal: "AA",
camel: "aA",
snake: "a_a",
header: "A-A",
},
{
input: "aa_bbb",
pascal: "AaBbb",
camel: "aaBbb",
snake: "aa_bbb",
header: "Aa-Bbb",
},
{
input: "aa_id",
Expand All @@ -147,18 +153,21 @@ func Test_splitJoin(t *testing.T) {
camel: "aaId",
camelGo: "aaID",
snake: "aa_id",
header: "Aa-ID",
},
{
input: "fooBar",
pascal: "FooBar",
camel: "fooBar",
snake: "foo_bar",
header: "Foo-Bar",
},
{
input: "FooBAR",
pascal: "FooBar",
camel: "fooBar",
snake: "foo_bar",
header: "Foo-Bar",
},
{
input: "fooUrl",
Expand All @@ -167,6 +176,7 @@ func Test_splitJoin(t *testing.T) {
camel: "fooUrl",
camelGo: "fooURL",
snake: "foo_url",
header: "Foo-URL",
},
{
input: "fooURL",
Expand All @@ -175,13 +185,15 @@ func Test_splitJoin(t *testing.T) {
camel: "fooUrl",
camelGo: "fooURL",
snake: "foo_url",
header: "Foo-URL",
},
{
input: "url10",
pascal: "Url10",
pascalGo: "URL10",
camel: "url10",
snake: "url_10",
header: "URL-10",
},
{
input: "url_id",
Expand All @@ -190,6 +202,7 @@ func Test_splitJoin(t *testing.T) {
camel: "urlId",
camelGo: "urlID",
snake: "url_id",
header: "URL-ID",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit b49cf25

Please sign in to comment.