pax_global_header00006660000000000000000000000064144536402570014524gustar00rootroot0000000000000052 comment=531aaa44de12ec166ceb71d9bdad7c8295e4235f strcase-0.3.0/000077500000000000000000000000001445364025700131705ustar00rootroot00000000000000strcase-0.3.0/.travis.yml000066400000000000000000000001521445364025700152770ustar00rootroot00000000000000sudo: false language: go go: - 1.10.x - 1.11.x - 1.12.x - 1.13.x - 1.14.x - 1.15.x - master strcase-0.3.0/LICENSE000066400000000000000000000021441445364025700141760ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Ian Coleman Copyright (c) 2018 Ma_124, Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, Subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or Substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. strcase-0.3.0/README.md000066400000000000000000000046121445364025700144520ustar00rootroot00000000000000# strcase [![Godoc Reference](https://godoc.org/github.com/iancoleman/strcase?status.svg)](http://godoc.org/github.com/iancoleman/strcase) [![Build Status](https://travis-ci.com/iancoleman/strcase.svg)](https://travis-ci.com/iancoleman/strcase) [![Coverage](http://gocover.io/_badge/github.com/iancoleman/strcase?0)](http://gocover.io/github.com/iancoleman/strcase) [![Go Report Card](https://goreportcard.com/badge/github.com/iancoleman/strcase)](https://goreportcard.com/report/github.com/iancoleman/strcase) strcase is a go package for converting string case to various cases (e.g. [snake case](https://en.wikipedia.org/wiki/Snake_case) or [camel case](https://en.wikipedia.org/wiki/CamelCase)) to see the full conversion table below. ## Example ```go s := "AnyKind of_string" ``` | Function | Result | |-------------------------------------------|----------------------| | `ToSnake(s)` | `any_kind_of_string` | | `ToSnakeWithIgnore(s, '.')` | `any_kind.of_string` | | `ToScreamingSnake(s)` | `ANY_KIND_OF_STRING` | | `ToKebab(s)` | `any-kind-of-string` | | `ToScreamingKebab(s)` | `ANY-KIND-OF-STRING` | | `ToDelimited(s, '.')` | `any.kind.of.string` | | `ToScreamingDelimited(s, '.', '', true)` | `ANY.KIND.OF.STRING` | | `ToScreamingDelimited(s, '.', ' ', true)` | `ANY.KIND OF.STRING` | | `ToCamel(s)` | `AnyKindOfString` | | `ToLowerCamel(s)` | `anyKindOfString` | ## Install ```bash go get -u github.com/iancoleman/strcase ``` ## Custom Acronyms for ToCamel && ToLowerCamel Often times text can contain specific acronyms which you need to be handled a certain way. Out of the box `strcase` treats the string "ID" as "Id" or "id" but there is no way to cater for every case in the wild. To configure your custom acronym globally you can use the following before running any conversion ```go import ( "github.com/iancoleman/strcase" ) func init() { // results in "Api" using ToCamel("API") // results in "api" using ToLowerCamel("API") strcase.ConfigureAcronym("API", "api") // results in "PostgreSQL" using ToCamel("PostgreSQL") // results in "postgreSQL" using ToLowerCamel("PostgreSQL") strcase.ConfigureAcronym("PostgreSQL", "PostgreSQL") } ``` strcase-0.3.0/acronyms.go000066400000000000000000000003751445364025700153570ustar00rootroot00000000000000package strcase import ( "sync" ) var uppercaseAcronym = sync.Map{} //"ID": "id", // ConfigureAcronym allows you to add additional words which will be considered acronyms func ConfigureAcronym(key, val string) { uppercaseAcronym.Store(key, val) } strcase-0.3.0/camel.go000066400000000000000000000044521445364025700146050ustar00rootroot00000000000000/* * The MIT License (MIT) * * Copyright (c) 2015 Ian Coleman * Copyright (c) 2018 Ma_124, * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, Subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or Substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package strcase import ( "strings" ) // Converts a string to CamelCase func toCamelInitCase(s string, initCase bool) string { s = strings.TrimSpace(s) if s == "" { return s } a, hasAcronym := uppercaseAcronym.Load(s) if hasAcronym { s = a.(string) } n := strings.Builder{} n.Grow(len(s)) capNext := initCase prevIsCap := false for i, v := range []byte(s) { vIsCap := v >= 'A' && v <= 'Z' vIsLow := v >= 'a' && v <= 'z' if capNext { if vIsLow { v += 'A' v -= 'a' } } else if i == 0 { if vIsCap { v += 'a' v -= 'A' } } else if prevIsCap && vIsCap && !hasAcronym { v += 'a' v -= 'A' } prevIsCap = vIsCap if vIsCap || vIsLow { n.WriteByte(v) capNext = false } else if vIsNum := v >= '0' && v <= '9'; vIsNum { n.WriteByte(v) capNext = true } else { capNext = v == '_' || v == ' ' || v == '-' || v == '.' } } return n.String() } // ToCamel converts a string to CamelCase func ToCamel(s string) string { return toCamelInitCase(s, true) } // ToLowerCamel converts a string to lowerCamelCase func ToLowerCamel(s string) string { return toCamelInitCase(s, false) } strcase-0.3.0/camel_test.go000066400000000000000000000104001445364025700156320ustar00rootroot00000000000000/* * The MIT License (MIT) * * Copyright (c) 2015 Ian Coleman * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, Subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or Substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package strcase import ( "testing" ) func toCamel(tb testing.TB) { cases := [][]string{ {"test_case", "TestCase"}, {"test.case", "TestCase"}, {"test", "Test"}, {"TestCase", "TestCase"}, {" test case ", "TestCase"}, {"", ""}, {"many_many_words", "ManyManyWords"}, {"AnyKind of_string", "AnyKindOfString"}, {"odd-fix", "OddFix"}, {"numbers2And55with000", "Numbers2And55With000"}, {"ID", "Id"}, {"CONSTANT_CASE", "ConstantCase"}, } for _, i := range cases { in := i[0] out := i[1] result := ToCamel(in) if result != out { tb.Errorf("%q (%q != %q)", in, result, out) } } } func TestToCamel(t *testing.T) { toCamel(t) } func BenchmarkToCamel(b *testing.B) { benchmarkCamelTest(b, toCamel) } func toLowerCamel(tb testing.TB) { cases := [][]string{ {"foo-bar", "fooBar"}, {"TestCase", "testCase"}, {"", ""}, {"AnyKind of_string", "anyKindOfString"}, {"AnyKind.of-string", "anyKindOfString"}, {"ID", "id"}, {"some string", "someString"}, {" some string", "someString"}, {"CONSTANT_CASE", "constantCase"}, } for _, i := range cases { in := i[0] out := i[1] result := ToLowerCamel(in) if result != out { tb.Errorf("%q (%q != %q)", in, result, out) } } } func TestToLowerCamel(t *testing.T) { toLowerCamel(t) } func TestCustomAcronymsToCamel(t *testing.T) { tests := []struct { name string acronymKey string acronymValue string expected string }{ { name: "API Custom Acronym", acronymKey: "API", acronymValue: "api", expected: "Api", }, { name: "ABCDACME Custom Acroynm", acronymKey: "ABCDACME", acronymValue: "AbcdAcme", expected: "AbcdAcme", }, { name: "PostgreSQL Custom Acronym", acronymKey: "PostgreSQL", acronymValue: "PostgreSQL", expected: "PostgreSQL", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { ConfigureAcronym(test.acronymKey, test.acronymValue) if result := ToCamel(test.acronymKey); result != test.expected { t.Errorf("expected custom acronym result %s, got %s", test.expected, result) } }) } } func TestCustomAcronymsToLowerCamel(t *testing.T) { tests := []struct { name string acronymKey string acronymValue string expected string }{ { name: "API Custom Acronym", acronymKey: "API", acronymValue: "api", expected: "api", }, { name: "ABCDACME Custom Acroynm", acronymKey: "ABCDACME", acronymValue: "AbcdAcme", expected: "abcdAcme", }, { name: "PostgreSQL Custom Acronym", acronymKey: "PostgreSQL", acronymValue: "PostgreSQL", expected: "postgreSQL", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { ConfigureAcronym(test.acronymKey, test.acronymValue) if result := ToLowerCamel(test.acronymKey); result != test.expected { t.Errorf("expected custom acronym result %s, got %s", test.expected, result) } }) } } func BenchmarkToLowerCamel(b *testing.B) { benchmarkCamelTest(b, toLowerCamel) } func benchmarkCamelTest(b *testing.B, fn func(testing.TB)) { for n := 0; n < b.N; n++ { fn(b) } } strcase-0.3.0/concurrency_test.go000066400000000000000000000032241445364025700171110ustar00rootroot00000000000000package strcase import ( "testing" ) func TestConcurrency(t *testing.T) { for i := 0; i < 10000; i++ { go doConvert() } } func doConvert() { var snakes = []string{"code", "exchange", "pe_ratio", "profit_margin", "updated_date"} for _, v := range snakes { _ = convertDatabaseNameToCamelCase(v) } } func convertDatabaseNameToCamelCase(d string) (s string) { ConfigureAcronym("price_book_mrq", "PriceBookMRQ") ConfigureAcronym("pe_ratio", "PERatio") ConfigureAcronym("peg_ratio", "PEGRatio") ConfigureAcronym("eps_estimate_current_year", "EPSEstimateCurrentYear") ConfigureAcronym("eps_estimate_next_year", "EPSEstimateNextYear") ConfigureAcronym("eps_estimate_next_quarter", "EPSNextQuarter") ConfigureAcronym("eps_estimate_current_quarter", "EPSEstimateCurrentQuarter") ConfigureAcronym("ebitda", "EBITDA") ConfigureAcronym("operating_margin_ttm", "OperatingMarginTTM") ConfigureAcronym("return_on_assets_ttm", "ReturnOnAssetsTTM") ConfigureAcronym("return_on_equity_ttm", "ReturnOnEquityTTM") ConfigureAcronym("revenue_ttm", "RevenueTTM") ConfigureAcronym("revenue_per_share_ttm", "RevenuePerShareTTM") ConfigureAcronym("quarterly_revenue_growth_yoy", "QuarterlyRevenueGrowthYOY") ConfigureAcronym("gross_profit_ttm", "GrossProfitTTM") ConfigureAcronym("diluted_eps_ttm", "DilutedEpsTTM") ConfigureAcronym("quarterly_earnings_growth_yoy", "QuarterlyEarningsGrowthYOY") ConfigureAcronym("two_hundred_day_ma", "TwoHundredDayMA") ConfigureAcronym("trailing_pe", "TrailingPE") ConfigureAcronym("forward_pe", "ForwardPE") ConfigureAcronym("price_sales_ttm", "PriceSalesTTM") ConfigureAcronym("price_book_mrq", "PriceBookMRQ") s = ToCamel(d) return } strcase-0.3.0/doc.go000066400000000000000000000013221445364025700142620ustar00rootroot00000000000000// Package strcase converts strings to various cases. See the conversion table below: // | Function | Result | // |---------------------------------|--------------------| // | ToSnake(s) | any_kind_of_string | // | ToScreamingSnake(s) | ANY_KIND_OF_STRING | // | ToKebab(s) | any-kind-of-string | // | ToScreamingKebab(s) | ANY-KIND-OF-STRING | // | ToDelimited(s, '.') | any.kind.of.string | // | ToScreamingDelimited(s, '.') | ANY.KIND.OF.STRING | // | ToCamel(s) | AnyKindOfString | // | ToLowerCamel(s) | anyKindOfString | package strcase strcase-0.3.0/go.mod000066400000000000000000000000561445364025700142770ustar00rootroot00000000000000module github.com/iancoleman/strcase go 1.16 strcase-0.3.0/snake.go000066400000000000000000000073131445364025700146240ustar00rootroot00000000000000/* * The MIT License (MIT) * * Copyright (c) 2015 Ian Coleman * Copyright (c) 2018 Ma_124, * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, Subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or Substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package strcase import ( "strings" ) // ToSnake converts a string to snake_case func ToSnake(s string) string { return ToDelimited(s, '_') } func ToSnakeWithIgnore(s string, ignore string) string { return ToScreamingDelimited(s, '_', ignore, false) } // ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE func ToScreamingSnake(s string) string { return ToScreamingDelimited(s, '_', "", true) } // ToKebab converts a string to kebab-case func ToKebab(s string) string { return ToDelimited(s, '-') } // ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE func ToScreamingKebab(s string) string { return ToScreamingDelimited(s, '-', "", true) } // ToDelimited converts a string to delimited.snake.case // (in this case `delimiter = '.'`) func ToDelimited(s string, delimiter uint8) string { return ToScreamingDelimited(s, delimiter, "", false) } // ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE // (in this case `delimiter = '.'; screaming = true`) // or delimited.snake.case // (in this case `delimiter = '.'; screaming = false`) func ToScreamingDelimited(s string, delimiter uint8, ignore string, screaming bool) string { s = strings.TrimSpace(s) n := strings.Builder{} n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters for i, v := range []byte(s) { vIsCap := v >= 'A' && v <= 'Z' vIsLow := v >= 'a' && v <= 'z' if vIsLow && screaming { v += 'A' v -= 'a' } else if vIsCap && !screaming { v += 'a' v -= 'A' } // treat acronyms as words, eg for JSONData -> JSON is a whole word if i+1 < len(s) { next := s[i+1] vIsNum := v >= '0' && v <= '9' nextIsCap := next >= 'A' && next <= 'Z' nextIsLow := next >= 'a' && next <= 'z' nextIsNum := next >= '0' && next <= '9' // add underscore if next letter case type is changed if (vIsCap && (nextIsLow || nextIsNum)) || (vIsLow && (nextIsCap || nextIsNum)) || (vIsNum && (nextIsCap || nextIsLow)) { prevIgnore := ignore != "" && i > 0 && strings.ContainsAny(string(s[i-1]), ignore) if !prevIgnore { if vIsCap && nextIsLow { if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap { n.WriteByte(delimiter) } } n.WriteByte(v) if vIsLow || vIsNum || nextIsNum { n.WriteByte(delimiter) } continue } } } if (v == ' ' || v == '_' || v == '-' || v == '.') && !strings.ContainsAny(string(v), ignore) { // replace space/underscore/hyphen/dot with delimiter n.WriteByte(delimiter) } else { n.WriteByte(v) } } return n.String() } strcase-0.3.0/snake_test.go000066400000000000000000000155171445364025700156700ustar00rootroot00000000000000/* * The MIT License (MIT) * * Copyright (c) 2015 Ian Coleman * Copyright (c) 2018 Ma_124, * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, Subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or Substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package strcase import ( "testing" ) func toSnake(tb testing.TB) { cases := [][]string{ {"testCase", "test_case"}, {"TestCase", "test_case"}, {"Test Case", "test_case"}, {" Test Case", "test_case"}, {"Test Case ", "test_case"}, {" Test Case ", "test_case"}, {"test", "test"}, {"test_case", "test_case"}, {"Test", "test"}, {"", ""}, {"ManyManyWords", "many_many_words"}, {"manyManyWords", "many_many_words"}, {"AnyKind of_string", "any_kind_of_string"}, {"numbers2and55with000", "numbers_2_and_55_with_000"}, {"JSONData", "json_data"}, {"userID", "user_id"}, {"AAAbbb", "aa_abbb"}, {"1A2", "1_a_2"}, {"A1B", "a_1_b"}, {"A1A2A3", "a_1_a_2_a_3"}, {"A1 A2 A3", "a_1_a_2_a_3"}, {"AB1AB2AB3", "ab_1_ab_2_ab_3"}, {"AB1 AB2 AB3", "ab_1_ab_2_ab_3"}, {"some string", "some_string"}, {" some string", "some_string"}, } for _, i := range cases { in := i[0] out := i[1] result := ToSnake(in) if result != out { tb.Errorf("%q (%q != %q)", in, result, out) } } } func TestToSnake(t *testing.T) { toSnake(t) } func BenchmarkToSnake(b *testing.B) { benchmarkSnakeTest(b, toSnake) } func toSnakeWithIgnore(tb testing.TB) { cases := [][]string{ {"testCase", "test_case"}, {"TestCase", "test_case"}, {"Test Case", "test_case"}, {" Test Case", "test_case"}, {"Test Case ", "test_case"}, {" Test Case ", "test_case"}, {"test", "test"}, {"test_case", "test_case"}, {"Test", "test"}, {"", ""}, {"ManyManyWords", "many_many_words"}, {"manyManyWords", "many_many_words"}, {"AnyKind of_string", "any_kind_of_string"}, {"numbers2and55with000", "numbers_2_and_55_with_000"}, {"JSONData", "json_data"}, {"AwesomeActivity.UserID", "awesome_activity.user_id", "."}, {"AwesomeActivity.User.Id", "awesome_activity.user.id", "."}, {"AwesomeUsername@Awesome.Com", "awesome_username@awesome.com", ".@"}, {"lets-ignore all.of dots-and-dashes", "lets-ignore_all.of_dots-and-dashes", ".-"}, } for _, i := range cases { in := i[0] out := i[1] var ignore string ignore = "" if len(i) == 3 { ignore = i[2] } result := ToSnakeWithIgnore(in, ignore) if result != out { istr := "" if len(i) == 3 { istr = " ignoring '" + i[2] + "'" } tb.Errorf("%q (%q != %q%s)", in, result, out, istr) } } } func TestToSnakeWithIgnore(t *testing.T) { toSnakeWithIgnore(t) } func BenchmarkToSnakeWithIgnore(b *testing.B) { benchmarkSnakeTest(b, toSnakeWithIgnore) } func toDelimited(tb testing.TB) { cases := [][]string{ {"testCase", "test@case"}, {"TestCase", "test@case"}, {"Test Case", "test@case"}, {" Test Case", "test@case"}, {"Test Case ", "test@case"}, {" Test Case ", "test@case"}, {"test", "test"}, {"test_case", "test@case"}, {"Test", "test"}, {"", ""}, {"ManyManyWords", "many@many@words"}, {"manyManyWords", "many@many@words"}, {"AnyKind of_string", "any@kind@of@string"}, {"numbers2and55with000", "numbers@2@and@55@with@000"}, {"JSONData", "json@data"}, {"userID", "user@id"}, {"AAAbbb", "aa@abbb"}, {"test-case", "test@case"}, } for _, i := range cases { in := i[0] out := i[1] result := ToDelimited(in, '@') if result != out { tb.Errorf("%q (%q != %q)", in, result, out) } } } func TestToDelimited(t *testing.T) { toDelimited(t) } func BenchmarkToDelimited(b *testing.B) { benchmarkSnakeTest(b, toDelimited) } func toScreamingSnake(tb testing.TB) { cases := [][]string{ {"testCase", "TEST_CASE"}, } for _, i := range cases { in := i[0] out := i[1] result := ToScreamingSnake(in) if result != out { tb.Errorf("%q (%q != %q)", in, result, out) } } } func TestToScreamingSnake(t *testing.T) { toScreamingSnake(t) } func BenchmarkToScreamingSnake(b *testing.B) { benchmarkSnakeTest(b, toScreamingSnake) } func toKebab(tb testing.TB) { cases := [][]string{ {"testCase", "test-case"}, } for _, i := range cases { in := i[0] out := i[1] result := ToKebab(in) if result != out { tb.Errorf("%q (%q != %q)", in, result, out) } } } func TestToKebab(t *testing.T) { toKebab(t) } func BenchmarkToKebab(b *testing.B) { benchmarkSnakeTest(b, toKebab) } func toScreamingKebab(tb testing.TB) { cases := [][]string{ {"testCase", "TEST-CASE"}, } for _, i := range cases { in := i[0] out := i[1] result := ToScreamingKebab(in) if result != out { tb.Errorf("%q (%q != %q)", in, result, out) } } } func TestToScreamingKebab(t *testing.T) { toScreamingKebab(t) } func BenchmarkToScreamingKebab(b *testing.B) { benchmarkSnakeTest(b, toScreamingKebab) } func toScreamingDelimited(tb testing.TB) { cases := [][]string{ {"testCase", "TEST.CASE"}, } for _, i := range cases { in := i[0] out := i[1] result := ToScreamingDelimited(in, '.', "", true) if result != out { tb.Errorf("%q (%q != %q)", in, result, out) } } } func TestToScreamingDelimited(t *testing.T) { toScreamingDelimited(t) } func BenchmarkToScreamingDelimited(b *testing.B) { benchmarkSnakeTest(b, toScreamingDelimited) } func toScreamingDelimitedWithIgnore(tb testing.TB) { cases := [][]string{ {"AnyKind of_string", "ANY.KIND OF.STRING", ".", " "}, } for _, i := range cases { in := i[0] out := i[1] delimiter := i[2][0] ignore := i[3][0] result := ToScreamingDelimited(in, delimiter, string(ignore), true) if result != out { istr := "" if len(i) == 4 { istr = " ignoring '" + i[3] + "'" } tb.Errorf("%q (%q != %q%s)", in, result, out, istr) } } } func TestToScreamingDelimitedWithIgnore(t *testing.T) { toScreamingDelimitedWithIgnore(t) } func BenchmarkToScreamingDelimitedWithIgnore(b *testing.B) { benchmarkSnakeTest(b, toScreamingDelimitedWithIgnore) } func benchmarkSnakeTest(b *testing.B, fn func(testing.TB)) { for n := 0; n < b.N; n++ { fn(b) } }