Skip to content

Commit d955b55

Browse files
committed
Merge pull request #5 from anronin/master
Use custom underscore function instead of Macro.undescore
2 parents 4f2ae39 + ec5f4e2 commit d955b55

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

lib/atomic_map.ex

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ defmodule AtomicMap do
4444
defp as_atom(s, false) when is_binary(s), do: s |> String.to_atom
4545
defp as_atom(s, _), do: s
4646

47-
def as_underscore(s, true) when is_binary(s), do: s |> Macro.underscore
48-
def as_underscore(s, true) when is_atom(s), do: s |> Atom.to_string |> as_underscore(true)
49-
def as_underscore(s, false), do: s
47+
defp as_underscore(s, true) when is_binary(s), do: s |> do_undescore
48+
defp as_underscore(s, true) when is_atom(s), do: s |> Atom.to_string |> as_underscore(true)
49+
defp as_underscore(s, false), do: s
50+
51+
defp do_undescore(s) do
52+
s
53+
|> String.replace(~r/([A-Z]+)([A-Z][a-z])/, "\\1_\\2")
54+
|> String.replace(~r/([a-z\d])([A-Z])/, "\\1_\\2")
55+
|> String.replace(~r/-/, "_")
56+
|> String.downcase
57+
end
5058
end

0 commit comments

Comments
 (0)