Current section
Files
Jump to
Current section
Files
lib/util/string_util.ex
defmodule Boring.Util.StringUtil do
@spec to_snake_case(value :: atom | String.t()) :: String.t()
def to_snake_case(atom) when is_atom(atom) do
atom
|> Atom.to_string()
|> to_snake_case()
end
def to_snake_case(string) when is_binary(string) do
string
|> String.replace(~r/([A-Z]+)([A-Z][a-z])/, "\\1_\\2")
|> String.replace(~r/([a-z\d])([A-Z])/, "\\1_\\2")
|> String.replace(~r/-/, "_")
|> String.downcase()
end
end