Skip to content

Instantly share code, notes, and snippets.

@rubikill
Last active February 12, 2017 04:17
Show Gist options
  • Save rubikill/3210957071fa19091761df6ad5e2dcf3 to your computer and use it in GitHub Desktop.
Save rubikill/3210957071fa19091761df6ad5e2dcf3 to your computer and use it in GitHub Desktop.
defmodule App do
def split_case(str) do
str
|> String.split("")
|> Enum.reduce({"", ""}, fn(x, {a, b})->
cond do
"A" <= x and x <= "Z" -> {a <> x, b}
"a" <= x and x <= "z" -> {a, b <> x}
true -> {a, b}
end
end)
end
end
App.split_case("Hello World")
@rubikill
Copy link
Author

defmodule App do
  @lower ["q", "w", "r", "t", "p", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m",
    "a", "à", "á", "ả", "ã", "ạ",
    "ă", "ằ", "ắ", "ẳ", "ẵ", "ặ",
    "â", "ầ", "ấ", "ẩ", "ẫ", "ậ",
    "e", "è", "é", "ẻ", "ẽ", "ẹ",
    "ê", "ề", "ế", "ể", "ễ", "ệ",
    "o", "ò", "ó", "ỏ", "õ", "ọ",
    "ô", "ồ", "ố", "ổ", "ỗ", "ộ",
    "ơ", "ờ", "ớ", "ở", "ỡ", "ợ",
    "i", "ì", "í", "ỉ", "ĩ", "ị",
    "u", "ù", "ú", "ủ", "ũ", "ụ",
    "u", "ừ", "ứ", "ử", "ữ", "ự",
    "y", "ỳ", "ý", "ỷ", "ỹ", "ỵ"]

  @upper ["Q", "W", "R", "T", "P", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M",
    "A", "À", "Á", "Ả", "Ã", "Ạ",
    "Ă", "Ằ", "Ắ", "Ẳ", "Ẵ", "Ặ",
    "Â", "Ầ", "Ấ", "Ẩ", "Ẫ", "Ậ",
    "E", "È", "É", "Ẻ", "Ẽ", "Ẹ",
    "Ê", "Ề", "Ế", "Ể", "Ễ", "Ệ",
    "O", "Ò", "Ó", "Ỏ", "Õ", "Ọ",
    "Ô", "Ồ", "Ố", "Ổ", "Ỗ", "Ộ",
    "Ơ", "Ờ", "Ớ", "Ở", "Ỡ", "Ợ",
    "I", "Ì", "Í", "Ỉ", "Ĩ", "Ị",
    "U", "Ù", "Ú", "Ủ", "Ũ", "Ụ",
    "U", "Ừ", "Ứ", "Ử", "Ữ", "Ự",
    "Y", "Ỳ", "Ý", "Ỷ", "Ỹ", "Ỵ"]

  def split_case(str) do
    str
    |> String.split("")
    |> Enum.reduce({"", ""}, fn(x, {a, b})->
      cond do
        "A" <= x and x <= "Z" -> {a <> x, b}
        "a" <= x and x <= "z" -> {a, b <> x}
        true -> {a, b}
      end
    end)
  end
  def split_case_vn(str) do
    str
    |> String.split("")
    |> Enum.reduce({"", ""}, fn(x, {a, b})->
      cond do
        x in @lower -> {a <> x, b}
        x in @upper -> {a, b <> x}
        true -> {a, b}
      end
    end)
  end
end

App.split_case("Hello World")
App.split_case_vn("Xin Chào")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment