|
5 | 5 |  |
6 | 6 |
|
7 | 7 |
|
8 | | -A small utility to convert deep Elixir maps with mixed string/atom keys to atom-only keyed maps. Optionally with a safe option, to prevent [atom space exhaustion of the Erlang VM](https://erlangcentral.org/wiki/index.php?title=String_Conversion_To_Atom). |
| 8 | +A small utility to convert deep Elixir maps with mixed string/atom keys to atom-only keyed maps. Optionally with a safe option, to prevent [atom space exhaustion of the Erlang VM](https://erlangcentral.org/wiki/index.php?title=String_Conversion_To_Atom). Since v0.8 it also supports conversion of keys from `CamelCase` to `under_score` format. |
9 | 9 |
|
10 | 10 | ## Usage |
11 | 11 |
|
12 | 12 |
|
13 | 13 | ```elixir |
| 14 | +# works with nested maps |
14 | 15 | iex> AtomicMap.convert(%{"a" => 2, "b" => %{"c" => 4}}, safe: true) |
15 | 16 | %{a: 2, b: %{c: 4}} |
16 | 17 |
|
| 18 | +# works with nested maps + lists + mixed key types (atoms + binaries) |
17 | 19 | iex> AtomicMap.convert([ %{"c" => 1}, %{:c => 2}, %{"c" => %{:b => 4}}], safe: true] |
18 | 20 | [%{c: 1}, %{c: 2}, %{c: %{b: 4}}] |
19 | 21 |
|
20 | 22 |
|
21 | | -iex> AtomicMap.convert(%{ "a" => [ %{"c" => 1}, %{"c" => 2}] }, safe: true] |
22 | | -%{a: [%{c: 1}, %{c: 2}] } |
23 | 23 |
|
| 24 | +# converts CamelCase to under_score by default (notice that you might have to turn 'safe' flag off) |
| 25 | +iex> AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false) |
| 26 | +%{camel_case: [%{c: 1}, %{c: 2}]} |
| 27 | + |
| 28 | +# underscoring can be turned off by passing `underscore: false` to opts |
| 29 | +iex> AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false, underscore: false ) |
| 30 | +%{CamelCase: [%{c: 1}, %{c: 2}]} |
24 | 31 | ``` |
25 | 32 |
|
26 | 33 |
|
|
0 commit comments