key_tools v0.4.1 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 snake_cased keys within the given Map or List to CamelCase

Deeply converts all snake_cased keys within the given Map or List to lowerCamelCase

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(struct)

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}}]

camelize_keys(struct)

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

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

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

Examples

iex(1)> KeyTools.camelize_keys %{“snake_key” => “is a snake”} %{“SnakeKey” => “is a snake”}

camelize_keys(struct, arg2)

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

For full details see the docs for KeyTools.camelize_keys/1.

Examples

iex(1)> KeyTools.camelize_keys %{“snake_key” => “is a snake”}, true %{“snakeKey” => “is a snake”}

stringify_keys(struct)

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(struct)

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 characters 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" => ":)"}}]