Skip to content

Commit

Permalink
Merge pull request #8 from stoewer/upper-snake-case
Browse files Browse the repository at this point in the history
Upper snake case
  • Loading branch information
stoewer authored Feb 26, 2020
2 parents d9b4e2d + 9920484 commit 7962b20
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![CircleCI](https://circleci.com/gh/stoewer/go-strcase/tree/master.svg?style=svg)](https://circleci.com/gh/stoewer/go-strcase/tree/master)
[![codecov](https://codecov.io/gh/stoewer/go-strcase/branch/master/graph/badge.svg)](https://codecov.io/gh/stoewer/go-strcase)
[![GoDoc](https://godoc.org/github.com/stoewer/go-strcase?status.svg)](https://godoc.org/github.com/stoewer/go-strcase)
[![GoDoc](https://godoc.org/github.com/stoewer/go-strcase?status.svg)](https://pkg.go.dev/github.com/stoewer/go-strcase)
---

Go strcase
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/stoewer/go-strcase

go 1.11

require github.com/stretchr/testify v1.4.0
require github.com/stretchr/testify v1.5.1
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down
7 changes: 6 additions & 1 deletion kebab.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ package strcase

// KebabCase converts a string into kebab case.
func KebabCase(s string) string {
return lowerDelimiterCase(s, '-')
return delimiterCase(s, '-', false)
}

// UpperKebabCase converts a string into kebab case with capital letters.
func UpperKebabCase(s string) string {
return delimiterCase(s, '-', true)
}
23 changes: 23 additions & 0 deletions kebab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ func TestKebabCase(t *testing.T) {
assert.Equal(t, snake, converted)
}
}

func TestUpperKebabCase(t *testing.T) {
data := map[string]string{
"": "",
"F": "F",
"Foo": "FOO",
"FooB": "FOO-B",
"FooID": "FOO-ID",
" FooBar\t": "FOO-BAR",
"HTTPStatusCode": "HTTP-STATUS-CODE",
"ParseURL.DoParse": "PARSE-URL.DO-PARSE",
"Convert Space": "CONVERT-SPACE",
"Convert-dash": "CONVERT-DASH",
"Skip___MultipleUnderscores": "SKIP-MULTIPLE-UNDERSCORES",
"Skip MultipleSpaces": "SKIP-MULTIPLE-SPACES",
"Skip---MultipleDashes": "SKIP-MULTIPLE-DASHES",
}

for camel, snake := range data {
converted := UpperKebabCase(camel)
assert.Equal(t, snake, converted)
}
}
24 changes: 17 additions & 7 deletions snake.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ import (

// SnakeCase converts a string into snake case.
func SnakeCase(s string) string {
return lowerDelimiterCase(s, '_')
return delimiterCase(s, '_', false)
}

// lowerDelimiterCase converts a string into snake_case or kebab-case depending on
// the delimiter passed in as second argument.
func lowerDelimiterCase(s string, delimiter rune) string {
// UpperSnakeCase converts a string into snake case with capital letters.
func UpperSnakeCase(s string) string {
return delimiterCase(s, '_', true)
}

// delimiterCase converts a string into snake_case or kebab-case depending on the delimiter passed
// as second argument. When upperCase is true the result will be UPPER_SNAKE_CASE or UPPER-KEBAB-CASE.
func delimiterCase(s string, delimiter rune, upperCase bool) string {
s = strings.TrimSpace(s)
buffer := make([]rune, 0, len(s)+3)

adjustCase := toLower
if upperCase {
adjustCase = toUpper
}

var prev rune
var curr rune
for _, next := range s {
Expand All @@ -29,9 +39,9 @@ func lowerDelimiterCase(s string, delimiter rune) string {
if isLower(prev) || (isUpper(prev) && isLower(next)) {
buffer = append(buffer, delimiter)
}
buffer = append(buffer, toLower(curr))
buffer = append(buffer, adjustCase(curr))
} else if curr != 0 {
buffer = append(buffer, curr)
buffer = append(buffer, adjustCase(curr))
}
prev = curr
curr = next
Expand All @@ -41,7 +51,7 @@ func lowerDelimiterCase(s string, delimiter rune) string {
if isUpper(curr) && isLower(prev) && prev != 0 {
buffer = append(buffer, delimiter)
}
buffer = append(buffer, toLower(curr))
buffer = append(buffer, adjustCase(curr))
}

return string(buffer)
Expand Down
23 changes: 23 additions & 0 deletions snake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ func TestSnakeCase(t *testing.T) {
assert.Equal(t, snake, converted)
}
}

func TestUpperSnakeCase(t *testing.T) {
data := map[string]string{
"": "",
"F": "F",
"Foo": "FOO",
"FooB": "FOO_B",
"FooID": "FOO_ID",
" FooBar\t": "FOO_BAR",
"HTTPStatusCode": "HTTP_STATUS_CODE",
"ParseURL.DoParse": "PARSE_URL.DO_PARSE",
"Convert Space": "CONVERT_SPACE",
"Convert-dash": "CONVERT_DASH",
"Skip___MultipleUnderscores": "SKIP_MULTIPLE_UNDERSCORES",
"Skip MultipleSpaces": "SKIP_MULTIPLE_SPACES",
"Skip---MultipleDashes": "SKIP_MULTIPLE_DASHES",
}

for camel, snake := range data {
converted := UpperSnakeCase(camel)
assert.Equal(t, snake, converted)
}
}

0 comments on commit 7962b20

Please sign in to comment.