File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
5058end
You can’t perform that action at this time.
0 commit comments