key_tools v0.2.0 KeyTools

Provides common functions for coercing a Map or List.

Summary

Functions

Deeply converts all string keys within the given Map or List to atoms

Deeply converts all keys within the given Map or List to strings

Deeply converts all camelCased keys within the given Map or List to snake_case

Functions

atomize_keys(map)

Deeply converts all string keys within the given Map or List to atoms.

Examples

iex(1)> KeyTools.atomize_keys %{“data” => ["Hello", "World!"]} %{data: ["Hello", "World!"]}

iex(2)> KeyTools.atomize_keys [%{"nested_data" => %{"deep" => :stuff}}] [%{nested_data: %{deep: :stuff}}]

stringify_keys(map)

Deeply converts all keys within the given Map or List to strings.

Examples

iex(1)> stringify_keys %{atom_key: :atom_value} %{“atom_key” => :atom_value}

iex(2)> stringify_keys [%{atom_key: %{42 => [%{another_key: 23}]}}] [%{"atom_key" => %{"42" => [%{"another_key" => 23}]}}]

underscore_keys(map)

Deeply converts all camelCased keys within the given Map or List to snake_case.

Affects only keys; values will remain unchanged. Works on string and atom keys.

The same limitations detailed in the docs for Macro.Underscore apply here, so be careful if there is potential for non-standard within your keys.

Examples

iex(1)> KeyTools.underscore_keys %{“scumbagKey” => “is a camel”} %{“scumbag_key” => “is a camel”}

iex(2)> KeyTools.underscore_keys [%{"nestedKeys" => %{"greatSuccess" => ":)"}}] [%{"nested_keys" => %{"great_success" => ":)"}}]