diff --git a/camel.go b/camel.go index 0def31b..bd20891 100644 --- a/camel.go +++ b/camel.go @@ -6,6 +6,7 @@ import ( // Converts a string to CamelCase func toCamelInitCase(s string, initCase bool) string { + s = addWordBoundariesToNumbers(s) s = strings.Trim(s, " ") n := "" capNext := initCase @@ -13,6 +14,9 @@ func toCamelInitCase(s string, initCase bool) string { if v >= 'A' && v <= 'Z' { n += string(v) } + if v >= '0' && v <= '9' { + n += string(v) + } if v >= 'a' && v <= 'z' { if capNext { n += strings.ToUpper(string(v)) diff --git a/camel_test.go b/camel_test.go index 7978623..a5242a6 100644 --- a/camel_test.go +++ b/camel_test.go @@ -14,6 +14,7 @@ func TestToCamel(t *testing.T) { []string{ "many_many_words", "ManyManyWords" }, []string{ "AnyKind of_string", "AnyKindOfString" }, []string{ "odd-fix", "OddFix" }, + []string{ "numbers2And55with000", "Numbers2And55With000" }, } for _, i := range cases { in := i[0] diff --git a/numbers.go b/numbers.go new file mode 100644 index 0000000..5aeddf3 --- /dev/null +++ b/numbers.go @@ -0,0 +1,14 @@ +package strcase + +import ( + "regexp" +) + +var numberSequence = regexp.MustCompile(`([a-zA-Z])(\d+)([a-zA-Z]?)`) +var numberReplacement = []byte(`$1 $2 $3`) + +func addWordBoundariesToNumbers(s string) string { + b := []byte(s) + b = numberSequence.ReplaceAll(b, numberReplacement) + return string(b) +} diff --git a/snake.go b/snake.go index 49adcd5..0ec6d64 100644 --- a/snake.go +++ b/snake.go @@ -7,6 +7,7 @@ import ( // Converts a string to snake_case func ToSnake(s string) string { + s = addWordBoundariesToNumbers(s) s = strings.Trim(s, " ") n := "" for i, v := range s { diff --git a/snake_test.go b/snake_test.go index a0d80e2..e30d85c 100644 --- a/snake_test.go +++ b/snake_test.go @@ -19,6 +19,7 @@ func TestToSnake(t *testing.T) { []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" }, } for _, i := range cases { in := i[0]