-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add AutoCLI Sane Kebab Case (#14316)
- Loading branch information
1 parent
cf9102f
commit 5573a2f
Showing
11 changed files
with
165 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015 Ian Coleman | ||
* Copyright (c) 2018 Ma_124, <github.com/Ma124> | ||
* Copyright (c) 2022 Cosmos SDK Developers, <github.com/cosmos/cosmos-sdk> | ||
* | ||
* 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" | ||
) | ||
|
||
// ToKebab converts a string to kebab-case | ||
func ToKebab(s string) string { | ||
return ToDelimited(s, '-', false) | ||
} | ||
|
||
// ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE | ||
func ToScreamingKebab(s string) string { | ||
return ToDelimited(s, '-', true) | ||
} | ||
|
||
// ToSnake converts a string to snake_case | ||
func ToSnake(s string) string { | ||
return ToDelimited(s, '_', false) | ||
} | ||
|
||
// ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE | ||
func ToScreamingSnake(s string) string { | ||
return ToDelimited(s, '_', true) | ||
} | ||
|
||
// ToDelimited converts a string to delimited.snake.case | ||
// (in this case `delimiter = '.'`) | ||
func ToDelimited(s string, delimiter uint8, 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)) { | ||
if vIsCap && nextIsLow { | ||
if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap { | ||
n.WriteByte(delimiter) | ||
} | ||
} | ||
n.WriteByte(v) | ||
if (vIsLow && nextIsCap) || (vIsNum && nextIsCap) || (vIsNum && nextIsLow) { | ||
n.WriteByte(delimiter) | ||
} | ||
continue | ||
} | ||
} | ||
|
||
if v == ' ' || v == '_' || v == '-' || v == '.' { | ||
// replace space/underscore/hyphen/dot with delimiter | ||
n.WriteByte(delimiter) | ||
} else { | ||
n.WriteByte(v) | ||
} | ||
} | ||
|
||
return n.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package strcase_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"cosmossdk.io/client/v2/internal/strcase" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func toKebab(t testing.TB) { | ||
cases := [][]string{ | ||
{"testCase", "test-case"}, | ||
{"TestCase", "test-case"}, | ||
{"Test Case", "test-case"}, | ||
{"TEST CASE", "test-case"}, | ||
{"TESTCase", "test-case"}, | ||
{"TESTCASE", "testcase"}, | ||
{"TEST_CASE", "test-case"}, | ||
{"Bech32", "bech32"}, | ||
{"Bech32Address", "bech32-address"}, | ||
{"Bech32_Address", "bech32-address"}, | ||
{"Bech32Address10", "bech32-address10"}, | ||
{"Bech32-Address10", "bech32-address10"}, | ||
{"SecondBech32Address10Foo", "second-bech32-address10-foo"}, | ||
} | ||
for _, i := range cases { | ||
in := i[0] | ||
out := i[1] | ||
result := strcase.ToKebab(in) | ||
assert.Equal(t, out, result, "ToKebab(%s) = %s, want %s", in, result, out) | ||
} | ||
} | ||
|
||
func TestToKebab(t *testing.T) { | ||
toKebab(t) | ||
} | ||
|
||
func BenchmarkToKebab(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
toKebab(b) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters