Skip to content

Commit 44d5ef8

Browse files
committed
underscore supports hyphens
problem: - for some special use cases we want to convert hyphens to underscores while underscoring solution: - combine Macro.underscore + replace "-" with "_" - unit test to cover this
1 parent d955b55 commit 44d5ef8

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

lib/atomic_map.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ defmodule AtomicMap do
5050

5151
defp do_undescore(s) do
5252
s
53-
|> String.replace(~r/([A-Z]+)([A-Z][a-z])/, "\\1_\\2")
54-
|> String.replace(~r/([a-z\d])([A-Z])/, "\\1_\\2")
53+
|> Macro.underscore
5554
|> String.replace(~r/-/, "_")
56-
|> String.downcase
5755
end
5856
end

test/atomic_map_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ defmodule AtomicMapTest do
6666
assert AtomicMap.convert(input, safe: false) == expected
6767
end
6868

69+
test "underscore flag works with hyphens" do
70+
input = %{ "first-key" => {1,2}}
71+
expected = %{first_key: {1, 2}}
72+
assert AtomicMap.convert(input, safe: false) == expected
73+
end
74+
6975
test "raises for not existing atoms" do
7076
assert_raise ArgumentError, fn ->
7177
input = %{"a" => 2, "b" => %{"c" => 4}, "__not___existing__" => 5}

0 commit comments

Comments
 (0)