From b30c1264effabdb1faf76234d1a1672c00ced540 Mon Sep 17 00:00:00 2001 From: Ian Coleman Date: Fri, 9 Oct 2015 20:13:05 +1100 Subject: [PATCH] Make repository public --- .travis.yml | 10 ++++++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 11 +++++++++++ camel.go | 30 ++++++++++++++++++++++++++++++ camel_test.go | 25 +++++++++++++++++++++++++ snake.go | 23 +++++++++++++++++++++++ snake_test.go | 31 +++++++++++++++++++++++++++++++ 7 files changed, 151 insertions(+) create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 camel.go create mode 100644 camel_test.go create mode 100644 snake.go create mode 100644 snake_test.go diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7eff6fa --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: go + +go: + - 1.3 + - 1.4 + - 1.5.1 + - release + +script: + - go test -v ./... diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d9e6fe3 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..41e7f54 --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/camel.go b/camel.go new file mode 100644 index 0000000..ffc52fc --- /dev/null +++ b/camel.go @@ -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 +} diff --git a/camel_test.go b/camel_test.go new file mode 100644 index 0000000..c6d8428 --- /dev/null +++ b/camel_test.go @@ -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 + "'") + } + } +} diff --git a/snake.go b/snake.go new file mode 100644 index 0000000..49adcd5 --- /dev/null +++ b/snake.go @@ -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 +} diff --git a/snake_test.go b/snake_test.go new file mode 100644 index 0000000..a0d80e2 --- /dev/null +++ b/snake_test.go @@ -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 + "'") + } + } +}