commit
e506e3ef73
@ -2,8 +2,9 @@
|
||||
[![Godoc Reference](https://godoc.org/github.com/iancoleman/strcase?status.svg)](http://godoc.org/github.com/iancoleman/strcase)
|
||||
[![Build Status](https://travis-ci.org/iancoleman/strcase.svg)](https://travis-ci.org/iancoleman/strcase)
|
||||
[![Coverage](http://gocover.io/_badge/github.com/iancoleman/strcase?0)](http://gocover.io/github.com/iancoleman/strcase)
|
||||
[![Go Report Card](https://goreportcard.com/badge/github.com/iancoleman/strcase)](https://goreportcard.com/report/github.com/iancoleman/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).
|
||||
strcase is a go package for converting string case to various cases (e.g. [snake case](https://en.wikipedia.org/wiki/Snake_case) or [camel case](https://en.wikipedia.org/wiki/CamelCase)) to see the full conversion table below.
|
||||
|
||||
## Example
|
||||
|
||||
|
4
camel.go
4
camel.go
@ -58,12 +58,12 @@ func toCamelInitCase(s string, initCase bool) string {
|
||||
return n
|
||||
}
|
||||
|
||||
// Converts a string to CamelCase
|
||||
// ToCamel converts a string to CamelCase
|
||||
func ToCamel(s string) string {
|
||||
return toCamelInitCase(s, true)
|
||||
}
|
||||
|
||||
// Converts a string to lowerCamelCase
|
||||
// ToLowerCamel converts a string to lowerCamelCase
|
||||
func ToLowerCamel(s string) string {
|
||||
if s == "" {
|
||||
return s
|
||||
|
@ -30,15 +30,15 @@ import (
|
||||
|
||||
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"},
|
||||
[]string{"odd-fix", "OddFix"},
|
||||
[]string{"numbers2And55with000", "Numbers2And55With000"},
|
||||
{"test_case", "TestCase"},
|
||||
{"test", "Test"},
|
||||
{"TestCase", "TestCase"},
|
||||
{" test case ", "TestCase"},
|
||||
{"", ""},
|
||||
{"many_many_words", "ManyManyWords"},
|
||||
{"AnyKind of_string", "AnyKindOfString"},
|
||||
{"odd-fix", "OddFix"},
|
||||
{"numbers2And55with000", "Numbers2And55With000"},
|
||||
}
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
@ -52,10 +52,10 @@ func TestToCamel(t *testing.T) {
|
||||
|
||||
func TestToLowerCamel(t *testing.T) {
|
||||
cases := [][]string{
|
||||
[]string{"foo-bar", "fooBar"},
|
||||
[]string{"TestCase", "testCase"},
|
||||
[]string{"", ""},
|
||||
[]string{"AnyKind of_string", "anyKindOfString"},
|
||||
{"foo-bar", "fooBar"},
|
||||
{"TestCase", "testCase"},
|
||||
{"", ""},
|
||||
{"AnyKind of_string", "anyKindOfString"},
|
||||
}
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
|
12
doc.go
Normal file
12
doc.go
Normal file
@ -0,0 +1,12 @@
|
||||
// Package strcase converts strings to various cases. See the conversion table below:
|
||||
// | Function | Result |
|
||||
// |---------------------------------|--------------------|
|
||||
// | ToSnake(s) | any_kind_of_string |
|
||||
// | ToScreamingSnake(s) | ANY_KIND_OF_STRING |
|
||||
// | ToKebab(s) | any-kind-of-string |
|
||||
// | ToScreamingKebab(s) | ANY-KIND-OF-STRING |
|
||||
// | ToDelimited(s, '.') | any.kind.of.string |
|
||||
// | ToScreamingDelimited(s, '.') | ANY.KIND.OF.STRING |
|
||||
// | ToCamel(s) | AnyKindOfString |
|
||||
// | ToLowerCamel(s) | anyKindOfString |
|
||||
package strcase
|
13
snake.go
13
snake.go
@ -23,39 +23,38 @@
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
// Package strcase converts strings to snake_case or CamelCase
|
||||
package strcase
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Converts a string to snake_case
|
||||
// ToSnake converts a string to snake_case
|
||||
func ToSnake(s string) string {
|
||||
return ToDelimited(s, '_')
|
||||
}
|
||||
|
||||
// Converts a string to SCREAMING_SNAKE_CASE
|
||||
// ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE
|
||||
func ToScreamingSnake(s string) string {
|
||||
return ToScreamingDelimited(s, '_', true)
|
||||
}
|
||||
|
||||
// Converts a string to kebab-case
|
||||
// ToKebab converts a string to kebab-case
|
||||
func ToKebab(s string) string {
|
||||
return ToDelimited(s, '-')
|
||||
}
|
||||
|
||||
// Converts a string to SCREAMING-KEBAB-CASE
|
||||
// ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE
|
||||
func ToScreamingKebab(s string) string {
|
||||
return ToScreamingDelimited(s, '-', true)
|
||||
}
|
||||
|
||||
// Converts a string to delimited.snake.case (in this case `del = '.'`)
|
||||
// ToDelimited converts a string to delimited.snake.case (in this case `del = '.'`)
|
||||
func ToDelimited(s string, del uint8) string {
|
||||
return ToScreamingDelimited(s, del, false)
|
||||
}
|
||||
|
||||
// Converts a string to SCREAMING.DELIMITED.SNAKE.CASE (in this case `del = '.'; screaming = true`) or delimited.snake.case (in this case `del = '.'; screaming = false`)
|
||||
// ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE (in this case `del = '.'; screaming = true`) or delimited.snake.case (in this case `del = '.'; screaming = false`)
|
||||
func ToScreamingDelimited(s string, del uint8, screaming bool) string {
|
||||
s = addWordBoundariesToNumbers(s)
|
||||
s = strings.Trim(s, " ")
|
||||
|
@ -31,23 +31,23 @@ import (
|
||||
|
||||
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"},
|
||||
[]string{"numbers2and55with000", "numbers_2_and_55_with_000"},
|
||||
[]string{"JSONData", "json_data"},
|
||||
[]string{"userID", "user_id"},
|
||||
[]string{"AAAbbb", "aa_abbb"},
|
||||
{"testCase", "test_case"},
|
||||
{"TestCase", "test_case"},
|
||||
{"Test Case", "test_case"},
|
||||
{" Test Case", "test_case"},
|
||||
{"Test Case ", "test_case"},
|
||||
{" Test Case ", "test_case"},
|
||||
{"test", "test"},
|
||||
{"test_case", "test_case"},
|
||||
{"Test", "test"},
|
||||
{"", ""},
|
||||
{"ManyManyWords", "many_many_words"},
|
||||
{"manyManyWords", "many_many_words"},
|
||||
{"AnyKind of_string", "any_kind_of_string"},
|
||||
{"numbers2and55with000", "numbers_2_and_55_with_000"},
|
||||
{"JSONData", "json_data"},
|
||||
{"userID", "user_id"},
|
||||
{"AAAbbb", "aa_abbb"},
|
||||
}
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
@ -61,24 +61,24 @@ func TestToSnake(t *testing.T) {
|
||||
|
||||
func TestToDelimited(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"},
|
||||
[]string{"numbers2and55with000", "numbers@2@and@55@with@000"},
|
||||
[]string{"JSONData", "json@data"},
|
||||
[]string{"userID", "user@id"},
|
||||
[]string{"AAAbbb", "aa@abbb"},
|
||||
[]string{"test-case", "test@case"},
|
||||
{"testCase", "test@case"},
|
||||
{"TestCase", "test@case"},
|
||||
{"Test Case", "test@case"},
|
||||
{" Test Case", "test@case"},
|
||||
{"Test Case ", "test@case"},
|
||||
{" Test Case ", "test@case"},
|
||||
{"test", "test"},
|
||||
{"test_case", "test@case"},
|
||||
{"Test", "test"},
|
||||
{"", ""},
|
||||
{"ManyManyWords", "many@many@words"},
|
||||
{"manyManyWords", "many@many@words"},
|
||||
{"AnyKind of_string", "any@kind@of@string"},
|
||||
{"numbers2and55with000", "numbers@2@and@55@with@000"},
|
||||
{"JSONData", "json@data"},
|
||||
{"userID", "user@id"},
|
||||
{"AAAbbb", "aa@abbb"},
|
||||
{"test-case", "test@case"},
|
||||
}
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
@ -92,7 +92,7 @@ func TestToDelimited(t *testing.T) {
|
||||
|
||||
func TestToScreamingSnake(t *testing.T) {
|
||||
cases := [][]string{
|
||||
[]string{"testCase", "TEST_CASE"},
|
||||
{"testCase", "TEST_CASE"},
|
||||
}
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
@ -106,7 +106,7 @@ func TestToScreamingSnake(t *testing.T) {
|
||||
|
||||
func TestToKebab(t *testing.T) {
|
||||
cases := [][]string{
|
||||
[]string{"testCase", "test-case"},
|
||||
{"testCase", "test-case"},
|
||||
}
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
@ -120,7 +120,7 @@ func TestToKebab(t *testing.T) {
|
||||
|
||||
func TestToScreamingKebab(t *testing.T) {
|
||||
cases := [][]string{
|
||||
[]string{"testCase", "TEST-CASE"},
|
||||
{"testCase", "TEST-CASE"},
|
||||
}
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
@ -134,7 +134,7 @@ func TestToScreamingKebab(t *testing.T) {
|
||||
|
||||
func TestToScreamingDelimited(t *testing.T) {
|
||||
cases := [][]string{
|
||||
[]string{"testCase", "TEST.CASE"},
|
||||
{"testCase", "TEST.CASE"},
|
||||
}
|
||||
for _, i := range cases {
|
||||
in := i[0]
|
||||
|
Loading…
Reference in New Issue
Block a user