From 44067336ae3b42cd97c86be08287cf674b7ca193 Mon Sep 17 00:00:00 2001 From: "likewei.ilike" Date: Sun, 29 Sep 2019 15:08:34 +0800 Subject: [PATCH] add method ToSnakeWithIgnore Change-Id: Id75f3d34d1ffc13a53351cdec79b2130cb8ffa66 --- snake.go | 15 +++++++++++---- snake_test.go | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/snake.go b/snake.go index b553b97..f4dd9c0 100644 --- a/snake.go +++ b/snake.go @@ -31,12 +31,16 @@ import ( // ToSnake converts a string to snake_case func ToSnake(s string) string { + return ToDelimited(s, '_') } +func ToSnakeWithIgnore(s string,ingore uint8) string { + return ToScreamingDelimited(s, '_',ingore,false) +} // ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE func ToScreamingSnake(s string) string { - return ToScreamingDelimited(s, '_', true) + return ToScreamingDelimited(s, '_',0, true) } // ToKebab converts a string to kebab-case @@ -46,16 +50,16 @@ func ToKebab(s string) string { // ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE func ToScreamingKebab(s string) string { - return ToScreamingDelimited(s, '-', true) + return ToScreamingDelimited(s, '-', 0,true) } // ToDelimited converts a string to delimited.snake.case (in this case `del = '.'`) func ToDelimited(s string, del uint8) string { - return ToScreamingDelimited(s, del, false) + return ToScreamingDelimited(s, del, 0,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 { +func ToScreamingDelimited(s string, del uint8,ingore uint8 ,screaming bool) string { s = addWordBoundariesToNumbers(s) s = strings.Trim(s, " ") n := "" @@ -67,6 +71,9 @@ func ToScreamingDelimited(s string, del uint8, screaming bool) string { if (v >= 'A' && v <= 'Z' && next >= 'a' && next <= 'z') || (v >= 'a' && v <= 'z' && next >= 'A' && next <= 'Z') { nextCaseIsChanged = true } + if(ingore >0 && i-1>=0 && s[i-1]==ingore &&nextCaseIsChanged ){ + nextCaseIsChanged=false + } } if i > 0 && n[len(n)-1] != del && nextCaseIsChanged { diff --git a/snake_test.go b/snake_test.go index 37ca332..830dc11 100644 --- a/snake_test.go +++ b/snake_test.go @@ -58,6 +58,40 @@ func TestToSnake(t *testing.T) { } } } +func TestToSnakeWithIgnore(t *testing.T) { + cases := [][]string{ + {"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"}, + {"AwesomeAcitvity.UserID", "awesome_acitvity.user_id","."}, + {"AwesomeAcitvity.User.Id", "awesome_acitvity.user.id","."}, + {"AwesomeUsername@Awesome.Com", "awesome_username@awesome._com","@"}, + } + for _, i := range cases { + in := i[0] + out := i[1] + var ingore uint8 + if len(i)==3{ + ingore=i[2][0] + } + result := ToSnakeWithIgnore(in,ingore) + if result != out { + t.Error("'" + in + "'('" + result + "' != '" + out + "')") + } + } +} func TestToDelimited(t *testing.T) { cases := [][]string{ @@ -139,7 +173,7 @@ func TestToScreamingDelimited(t *testing.T) { for _, i := range cases { in := i[0] out := i[1] - result := ToScreamingDelimited(in, '.', true) + result := ToScreamingDelimited(in, '.',0, true) if result != out { t.Error("'" + result + "' != '" + out + "'") }