Skip to content

Commit

Permalink
fix: Don't panic when trying ToSnake on s character (#1816)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Jul 24, 2024
1 parent 639efc2 commit 30e02da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion caser/caser.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (c *Caser) ToSnake(s string) string {
// append the last word
if s[lastPos:] != "" {
// handle plurals of initialisms like CDNs, ARNs, IDs
if w := s[lastPos:]; w == "s" {
if w := s[lastPos:]; w == "s" && words != nil {
words[len(words)-1] = words[len(words)-1] + w
} else {
words = append(words, s[lastPos:])
Expand Down
5 changes: 5 additions & 0 deletions caser/caser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func Test_ToSnake(t *testing.T) {
{Camel: " H e l l o", Snake: "h_e_l_l_o"},
{Camel: " H e l l o ", Snake: "h_e_l_l_o"},
{Camel: "Hello World", Snake: "hello_world"},
{Camel: "s", Snake: "s"},
{Camel: "S", Snake: "s"},
}
t.Parallel()
c := New()
Expand All @@ -67,6 +69,7 @@ func Test_ToCamel(t *testing.T) {
{Camel: "testCamelCaseLongString", Snake: "test_camel_case_long_string"},
{Camel: "testCamelCaseLongString", Snake: "test_camel_case_long_string"},
{Camel: "testIPv4", Snake: "test_ipv4"},
{Camel: "s", Snake: "s"},
}
t.Parallel()
c := New()
Expand Down Expand Up @@ -94,6 +97,7 @@ func Test_ToTitle(t *testing.T) {
{Title: "Test IPv4", Snake: "test_ipv4"},
{Title: "AWS Test Table", Snake: "aws_test_table"},
{Title: "Gcp Test Table", Snake: "gcp_test_table"}, // no exception specified
{Title: "S", Snake: "s"},
}
t.Parallel()
c := New(WithCustomExceptions(map[string]string{
Expand Down Expand Up @@ -125,6 +129,7 @@ func Test_ToPascal(t *testing.T) {
{Pascal: "TestIPv4", Snake: "test_ipv4"},
{Pascal: "Ec2", Snake: "ec2"},
{Pascal: "S3", Snake: "s3"},
{Pascal: "S", Snake: "s"},
}
t.Parallel()
c := New()
Expand Down

2 comments on commit 30e02da

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱️ Benchmark results

  • Glob-8 ns/op: 91.55

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱️ Benchmark results

  • Glob-8 ns/op: 92.4

Please sign in to comment.