From d8275f25354ae28ae89eb2c2b1d33cdae78609c5 Mon Sep 17 00:00:00 2001 From: bestplay <476128561@qq.com> Date: Fri, 27 Apr 2018 15:32:37 +0800 Subject: [PATCH 1/3] Update snake.go Consider userID to user_id --- snake.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/snake.go b/snake.go index 1af946b..d73cab1 100644 --- a/snake.go +++ b/snake.go @@ -12,12 +12,12 @@ func ToSnake(s string) string { n := "" for i, v := range s { // treat acronyms as words, eg for JSONData -> JSON is a whole word - nextIsCapital := false - if i + 1 < len(s) { - w := s[i+1] - nextIsCapital = w >= 'A' && w <= 'Z' + preIsCapital := false + if i - 1 > 0 { + w := s[i-1] + preIsCapital = w >= 'A' && w <= 'Z' } - if i > 0 && v >= 'A' && v <= 'Z' && n[len(n)-1] != '_' && !nextIsCapital { + if i > 0 && v >= 'A' && v <= 'Z' && n[len(n)-1] != '_' && !preIsCapital { // add underscore if next letter is a capital n += "_" + string(v) } else if v == ' ' { From f2ad438e9efb0b2c64d0e77ca2bf7ef0fc151988 Mon Sep 17 00:00:00 2001 From: bestplay <476128561@qq.com> Date: Fri, 27 Apr 2018 15:43:56 +0800 Subject: [PATCH 2/3] Update snake.go --- snake.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snake.go b/snake.go index d73cab1..e8bf881 100644 --- a/snake.go +++ b/snake.go @@ -13,7 +13,7 @@ func ToSnake(s string) string { for i, v := range s { // treat acronyms as words, eg for JSONData -> JSON is a whole word preIsCapital := false - if i - 1 > 0 { + if i > 0 { w := s[i-1] preIsCapital = w >= 'A' && w <= 'Z' } From 7c09721c5bf57ba28c6330fb33b3fcf0bfa2371c Mon Sep 17 00:00:00 2001 From: richard <476128561@qq.com> Date: Sat, 19 May 2018 16:27:10 +0800 Subject: [PATCH 3/3] Support from lowercase to continuous uppercase --- snake.go | 21 ++++++++++++++------- snake_test.go | 2 ++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/snake.go b/snake.go index e8bf881..23c7670 100644 --- a/snake.go +++ b/snake.go @@ -12,14 +12,21 @@ func ToSnake(s string) string { n := "" for i, v := range s { // treat acronyms as words, eg for JSONData -> JSON is a whole word - preIsCapital := false - if i > 0 { - w := s[i-1] - preIsCapital = w >= 'A' && w <= 'Z' + nextCaseIsChanged := false + if i+1 < len(s) { + next := s[i+1] + if (v >= 'A' && v <= 'Z' && next >= 'a' && next <= 'z') || (v >= 'a' && v <= 'z' && next >= 'A' && next <= 'Z') { + nextCaseIsChanged = true + } } - if i > 0 && v >= 'A' && v <= 'Z' && n[len(n)-1] != '_' && !preIsCapital { - // add underscore if next letter is a capital - n += "_" + string(v) + + if i > 0 && n[len(n)-1] != '_' && nextCaseIsChanged { + // add underscore if next letter case type is changed + if v >= 'A' && v <= 'Z' { + n += "_" + string(v) + } else if v >= 'a' && v <= 'z' { + n += string(v) + "_" + } } else if v == ' ' { // replace spaces with underscores n += "_" diff --git a/snake_test.go b/snake_test.go index a90ad4f..16d0143 100644 --- a/snake_test.go +++ b/snake_test.go @@ -21,6 +21,8 @@ func TestToSnake(t *testing.T) { []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"}, } for _, i := range cases { in := i[0]