Make repository public
This commit is contained in:
commit
b30c1264ef
10
.travis.yml
Normal file
10
.travis.yml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.3
|
||||||
|
- 1.4
|
||||||
|
- 1.5.1
|
||||||
|
- release
|
||||||
|
|
||||||
|
script:
|
||||||
|
- go test -v ./...
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
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.
|
11
README.md
Normal file
11
README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# strcase
|
||||||
|
|
||||||
|
strcase is a go package for converting string case to [snake case](https://en.wikipedia.org/wiki/Snake_case) or [camel case](https://en.wikipedia.org/wiki/CamelCase).
|
||||||
|
|
||||||
|
# Example
|
||||||
|
|
||||||
|
```
|
||||||
|
x := "AnyKind of_string"
|
||||||
|
xSnake := strcase.ToSnake(x) // any_kind_of_string
|
||||||
|
xCamel := strcase.ToCamel(x) // AnyKindOfString
|
||||||
|
```
|
30
camel.go
Normal file
30
camel.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package strcase
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Converts a string to CamelCase
|
||||||
|
func ToCamel(s string) string {
|
||||||
|
s = strings.Trim(s, " ")
|
||||||
|
n := ""
|
||||||
|
capNext := true
|
||||||
|
for _, v := range s {
|
||||||
|
if v >= 'A' && v <= 'Z' {
|
||||||
|
n += string(v)
|
||||||
|
}
|
||||||
|
if v >= 'a' && v <= 'z' {
|
||||||
|
if capNext {
|
||||||
|
n += strings.ToUpper(string(v))
|
||||||
|
} else {
|
||||||
|
n += string(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if v == '_' || v == ' ' {
|
||||||
|
capNext = true
|
||||||
|
} else {
|
||||||
|
capNext = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
25
camel_test.go
Normal file
25
camel_test.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package strcase
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToCamel(t *testing.T) {
|
||||||
|
cases := [][]string{
|
||||||
|
[]string{ "test_case", "TestCase" },
|
||||||
|
[]string{ "test", "Test" },
|
||||||
|
[]string{ "TestCase", "TestCase" },
|
||||||
|
[]string{ " test case ", "TestCase" },
|
||||||
|
[]string{ "", "" },
|
||||||
|
[]string{ "many_many_words", "ManyManyWords" },
|
||||||
|
[]string{ "AnyKind of_string", "AnyKindOfString" },
|
||||||
|
}
|
||||||
|
for _, i := range cases {
|
||||||
|
in := i[0]
|
||||||
|
out := i[1]
|
||||||
|
result := ToCamel(in)
|
||||||
|
if result != out {
|
||||||
|
t.Error("'" + result + "' != '" + out + "'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
snake.go
Normal file
23
snake.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Package strcase converts strings to snake_case or CamelCase
|
||||||
|
package strcase
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Converts a string to snake_case
|
||||||
|
func ToSnake(s string) string {
|
||||||
|
s = strings.Trim(s, " ")
|
||||||
|
n := ""
|
||||||
|
for i, v := range s {
|
||||||
|
if i > 0 && v >= 'A' && v <= 'Z' && n[len(n)-1] != '_' {
|
||||||
|
n += "_" + string(v)
|
||||||
|
} else if v == ' ' {
|
||||||
|
n += "_"
|
||||||
|
} else {
|
||||||
|
n = n + string(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
n = strings.ToLower(n)
|
||||||
|
return n
|
||||||
|
}
|
31
snake_test.go
Normal file
31
snake_test.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package strcase
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToSnake(t *testing.T) {
|
||||||
|
cases := [][]string{
|
||||||
|
[]string{ "testCase", "test_case" },
|
||||||
|
[]string{ "TestCase", "test_case" },
|
||||||
|
[]string{ "Test Case", "test_case" },
|
||||||
|
[]string{ " Test Case", "test_case" },
|
||||||
|
[]string{ "Test Case ", "test_case" },
|
||||||
|
[]string{ " Test Case ", "test_case" },
|
||||||
|
[]string{ "test", "test" },
|
||||||
|
[]string{ "test_case", "test_case" },
|
||||||
|
[]string{ "Test", "test" },
|
||||||
|
[]string{ "", "" },
|
||||||
|
[]string{ "ManyManyWords", "many_many_words" },
|
||||||
|
[]string{ "manyManyWords", "many_many_words" },
|
||||||
|
[]string{ "AnyKind of_string", "any_kind_of_string" },
|
||||||
|
}
|
||||||
|
for _, i := range cases {
|
||||||
|
in := i[0]
|
||||||
|
out := i[1]
|
||||||
|
result := ToSnake(in)
|
||||||
|
if result != out {
|
||||||
|
t.Error("'" + result + "' != '" + out + "'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user