KeyConvert v0.2.0 KeyConvert View Source

KeyConvert allows transforming the keys of maps to another case.

Atom keys will be converted to Strings as atoms are not garbage-collected and are not meant for dynamically generated data.

Key transformations are done recursively by default but can be disabled through options.

Shared options

All of the convenience functions accept the following options:

  • :mode - determines which keys are affected by the transformation

    • :deep - keys are transformed recursively (default)
    • :shallow - only first-level keys are transformed

The following shows an illustration of the usage of shared options:

input = %{contactInfo: %{emailAddress: "email@example.com"}}

KeyConvert.snake_case(input, mode: :deep)
# %{"contact_info" => %{"email_address" => "email@example.com"}}

KeyConvert.snake_case(input, mode: :shallow)
# input = %{"contact_info" => %{emailAddress: "email@example.com"}}

Link to this section Summary

Functions

Converts the keys to camel case

Converts the keys based on converter function provided

Renames the keys based on rename_map as lookup

Converts the keys to snake case

Link to this section Functions

Link to this function camelize(map, options \\ []) View Source

Converts the keys to camel case.

Examples

iex> KeyConvert.camelize(%{total_amount: 500})
%{"totalAmount" => 500}

iex> KeyConvert.camelize(%{
...>   contact_info: %{email_address: "email@example.com"}
...> })
%{"contactInfo" => %{"emailAddress" => "email@example.com"}}
Link to this function convert(map, converter, options \\ []) View Source

Converts the keys based on converter function provided.

Converter function should be able to take a key as an input and return a new key which will be used for the converted Map.

Examples

iex> append_change = fn key -> key <> ".changed" end
iex> KeyConvert.convert(%{"total_amount" => 500}, append_change)
%{"total_amount.changed" => 500}
Link to this function rename(map, rename_map, options \\ []) View Source

Renames the keys based on rename_map as lookup.

Keys not included in rename_map will not be changed.

Examples

iex> KeyConvert.rename(
...>   %{amount: 500, currency: "PHP"},
...>   %{amount: :value}
...> )
%{value: 500, currency: "PHP"}
Link to this function snake_case(map, options \\ []) View Source

Converts the keys to snake case.

Examples

iex> KeyConvert.snake_case(%{totalAmount: 500})
%{"total_amount" => 500}

iex> KeyConvert.snake_case(%{
...>   contactInfo: %{emailAddress: "email@example.com"}
...> })
%{"contact_info" => %{"email_address" => "email@example.com"}}