From b49cf25e976b8ae1b83f004f2d9755f9e733ec13 Mon Sep 17 00:00:00 2001 From: Sebastien Lavoie Date: Fri, 27 Aug 2021 11:36:20 -0400 Subject: [PATCH] Add ToHeaderField --- strcase/id.go | 5 +++++ strcase/id_test.go | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/strcase/id.go b/strcase/id.go index e9e5402..a120c15 100644 --- a/strcase/id.go +++ b/strcase/id.go @@ -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) diff --git a/strcase/id_test.go b/strcase/id_test.go index bb23e47..1bb29b2 100644 --- a/strcase/id_test.go +++ b/strcase/id_test.go @@ -106,6 +106,7 @@ func Test_splitJoin(t *testing.T) { pascal string pascalGo string snake string + header string }{ { // everything empty @@ -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", @@ -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", @@ -167,6 +176,7 @@ func Test_splitJoin(t *testing.T) { camel: "fooUrl", camelGo: "fooURL", snake: "foo_url", + header: "Foo-URL", }, { input: "fooURL", @@ -175,6 +185,7 @@ func Test_splitJoin(t *testing.T) { camel: "fooUrl", camelGo: "fooURL", snake: "foo_url", + header: "Foo-URL", }, { input: "url10", @@ -182,6 +193,7 @@ func Test_splitJoin(t *testing.T) { pascalGo: "URL10", camel: "url10", snake: "url_10", + header: "URL-10", }, { input: "url_id", @@ -190,6 +202,7 @@ func Test_splitJoin(t *testing.T) { camel: "urlId", camelGo: "urlID", snake: "url_id", + header: "URL-ID", }, } for _, tt := range tests {