Created
January 17, 2020 00:45
-
-
Save garthk/9f6445d40736750ec9b391c569094157 to your computer and use it in GitHub Desktop.
Pascal case conversion in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@doc "Convert a string or atom to a pascal case string" | |
@spec pascal_case(String.t() | atom()) :: String.t() | |
def pascal_case(name) | |
def pascal_case(name) when is_atom(name), do: name |> Atom.to_string() |> pascal_case() | |
def pascal_case(name) when is_binary(name) do | |
~r{(\W|_)+} | |
|> Regex.split(name) | |
|> Enum.map(&capitalise/1) | |
|> Enum.to_list() | |
|> IO.iodata_to_binary() | |
end | |
defp capitalise(""), do: "" | |
defp capitalise(<<head::utf8, tail::binary>>), do: [String.upcase(<<head::utf8>>), tail] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment