Skip to content

A small utility to convert deep Elixir maps with mixed string/atom keys to atom-only keyed maps.

Notifications You must be signed in to change notification settings

lachezar/atomic_map

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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}]} # in case you have extra large maps and you wish to use the faster custom transformation to underscore, you can pass `high_perf: true` AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false, high_perf: true) %{camel_case: [%{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}]} # you can choose to ignore unknown atoms (so it won't blow up with safe option...) AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: true, ignore: true) %{"camel_case" => [%{c: 1}, %{c: 2}]}

Installation

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

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

Todo:

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

Benchmark

$ mix bench 

About

A small utility to convert deep Elixir maps with mixed string/atom keys to atom-only keyed maps.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Elixir 100.0%