Skip to content

Latest commit

 

History

History
49 lines (32 loc) · 1.84 KB

README.md

File metadata and controls

49 lines (32 loc) · 1.84 KB

AtomicMap

Build status Hex version Hex downloads

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. Since v0.8 it also supports conversion of keys from CamelCase to under_score format.

Usage

# works with nested maps
iex> AtomicMap.convert(%{"a" => 2, "b" => %{"c" => 4}}, safe: true)
%{a: 2, b: %{c: 4}}

# works with nested maps + lists + mixed key types (atoms + binaries)
iex> AtomicMap.convert([ %{"c" => 1}, %{:c => 2}, %{"c" => %{:b => 4}}], safe: true]
[%{c: 1}, %{c: 2}, %{c: %{b: 4}}]

# converts CamelCase to under_score by default (notice that you might have to turn 'safe' flag off)
iex> AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false)
%{camel_case: [%{c: 1}, %{c: 2}]}

# underscoring can be turned off by passing `underscore: false` to opts
iex> AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false, underscore: false )
%{CamelCase: [%{c: 1}, %{c: 2}]}

# hyphens are replaced
iex> AtomicMap.convert(%{ "some-key" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false, underscore: true )
%{some_key: [%{c: 1}, %{c: 2}]}

Installation

  1. Add atomic_map to your list of dependencies in mix.exs:

    def deps do [{:atomic_map, "~> 0.8"}] end

Todo:

  • maybe allow direct conversion to a struct, like Poison does it: as: %SomeStruct{}...

Benchmark

$ mix bench