Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve camel case implementation #5

Merged
merged 9 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: 2.1
orbs:
codecov: codecov/[email protected]
jobs:
build:
docker:
- image: golang:1.13
working_directory: /work
steps:
- checkout
- run:
name: Install golangci-lint
environment:
GOLANGCI_LINT_VERSION: 1.21.0
command: |
wget -q https://github.com/golangci/golangci-lint/releases/download/v$GOLANGCI_LINT_VERSION/golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz \
-O /tmp/golangci-lint.tar.gz
tar --strip-components=1 -C $GOPATH/bin -xzf /tmp/golangci-lint.tar.gz
golangci-lint --version
- run:
name: Code analysis
command: golangci-lint run -v --config .golangci.yml ./...
- run:
name: Test
command: go test -mod=readonly -v -race -count=1 -cover -coverprofile=profile.cov ./...
- codecov/upload:
file: /work/profile.cov
26 changes: 26 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
run:
deadline: 10m

linters:
enable:
- dupl
- goconst
- gocyclo
- godox
- gosec
- interfacer
- lll
- maligned
- misspell
- prealloc
- stylecheck
- unconvert
- unparam
- errcheck
- golint
- gofmt
disable: []
fast: false

issues:
exclude-use-default: false
3 changes: 0 additions & 3 deletions .gometalinter.json

This file was deleted.

20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

17 changes: 5 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Build Status](https://travis-ci.org/stoewer/go-strcase.svg?branch=master)](https://travis-ci.org/stoewer/go-strcase)
[![Coverage Status](https://coveralls.io/repos/github/stoewer/go-strcase/badge.svg?branch=master)](https://coveralls.io/github/stoewer/go-strcase?branch=master)
[![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)
---

Expand Down Expand Up @@ -42,16 +42,9 @@ Dependencies
Run linters and unit tests
--------------------------

Since some of the linters ran by gometalinter don't support go modules yet, test dependencies have to be
loaded to the vendor directory first and gometalinter itself must run with disabled module support:
To run the static code analysis, linters and tests use the following commands:

```
go mod vendor
GO111MODULE=off gometalinter --config=.gometalinter.json --deadline=10m .
golangci-lint run --config .golangci.yml ./...
go test ./...
```

To run the test use the following commands:

```
go test .
```
10 changes: 5 additions & 5 deletions camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ func LowerCamelCase(s string) string {
}

func camelCase(s string, upper bool) string {
s = strings.TrimSpace(SnakeCase(s))
s = strings.TrimSpace(s)
buffer := make([]rune, 0, len(s))

var prev rune
for _, curr := range s {
stringIter(s, func(prev, curr, next rune) {
if !isDelimiter(curr) {
if isDelimiter(prev) || (upper && prev == 0) {
buffer = append(buffer, toUpper(curr))
} else if isLower(prev) {
buffer = append(buffer, curr)
} else {
buffer = append(buffer, toLower(curr))
}
}
prev = curr
}
})

return string(buffer)
}
4 changes: 4 additions & 0 deletions camel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func TestUpperCamelCase(t *testing.T) {
"f": "F",
"foo": "Foo",
"fooBar": "FooBar",
"FooBarBla": "FooBarBla",
"foo_barBla": "FooBarBla",
" foo_bar\n": "FooBar",
" foo-bar\t": "FooBar",
" foo bar\r": "FooBar",
Expand All @@ -36,6 +38,8 @@ func TestLowerCamelCase(t *testing.T) {
"F": "f",
"foo": "foo",
"FooBar": "fooBar",
"fooBarBla": "fooBarBla",
"foo_barBla": "fooBarBla",
" foo_bar\n": "fooBar",
" foo-bar\t": "fooBar",
" foo bar\r": "fooBar",
Expand Down
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module github.com/stoewer/go-strcase

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)
go 1.11

require github.com/stretchr/testify v1.4.0
13 changes: 9 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
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=
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=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
27 changes: 27 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,30 @@ func isSpace(ch rune) bool {
func isDelimiter(ch rune) bool {
return ch == '-' || ch == '_' || isSpace(ch)
}

// iterFunc is a callback that is called fro a specific position in a string. Its arguments are the
// rune at the respective string position as well as the previous and the next rune. If curr is at the
// first position of the string prev is zero. If curr is at the end of the string next is zero.
type iterFunc func(prev, curr, next rune)

// stringIter iterates over a string, invoking the callback for every single rune in the string.
func stringIter(s string, callback iterFunc) {
var prev rune
var curr rune
for _, next := range s {
if curr == 0 {
prev = curr
curr = next
continue
}

callback(prev, curr, next)

prev = curr
curr = next
}

if len(s) > 0 {
callback(prev, curr, 0)
}
}