diff --git a/lib/recase/cases/generic.ex b/lib/recase/cases/generic.ex index d4ccc58..2489a6b 100644 --- a/lib/recase/cases/generic.ex +++ b/lib/recase/cases/generic.ex @@ -107,16 +107,19 @@ defmodule Recase.Generic do end) Enum.each(?A..?Z, fn char -> - defp do_split(<>, {"", acc}), - do: do_split(rest, {<>, acc}) + defp do_split(<> = input, {"", acc}) do + {upcase_streak, rest} = upcase_streak(input, "") + + case byte_size(upcase_streak) do + 1 -> + do_split(rest, {<>, acc}) - defp do_split(<>, {curr, acc}) do - <> = String.reverse(curr) + 2 -> + <> = upcase_streak + do_split(rest, {<>, [c1 | acc]}) - if c in ?A..?Z do - do_split(rest, {curr <> <>, acc}) - else - do_split(rest, {<>, [curr | acc]}) + _ -> + do_split(rest, {<>, acc}) end end end) @@ -145,4 +148,11 @@ defmodule Recase.Generic do do_split(rest, {curr <> <>, acc}) end end + + Enum.each(?A..?Z, fn char -> + defp upcase_streak(<>, curr), + do: upcase_streak(rest, curr <> <>) + end) + + defp upcase_streak(rest, upcase_streak), do: {upcase_streak, rest} end diff --git a/test/recase_test/snake_case_test.exs b/test/recase_test/snake_case_test.exs index 5734dcd..2d1167f 100644 --- a/test/recase_test/snake_case_test.exs +++ b/test/recase_test/snake_case_test.exs @@ -16,6 +16,7 @@ defmodule Recase.SnakeCaseTest do assert convert("--snake-case--") == "snake_case" assert convert("snake#case") == "snake_case" assert convert("snake?!case") == "snake_case" + assert convert("SnakeACase") == "snake_a_case" end test "should return single letter" do