KeyConvert v0.5.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 string keys to atom. Non string keys are unaffected. Use this function with caution as atoms are not garbage collected.
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.
Converts atom keys to string. Non atom keys are unaffected.
Link to this section Functions
atomize(map, options \\ []) View Source
Converts string keys to atom. Non string keys are unaffected. Use this function with caution as atoms are not garbage collected.
Examples
iex> KeyConvert.atomize(%{"amount" => 100})
%{amount: 100}
iex> KeyConvert.atomize(%{100 => "amount"})
%{100 => "amount"}
camelize(collection, 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"}}
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}
rename(collection, 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"}
snake_case(collection, 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"}}
stringify(map, options \\ []) View Source
Converts atom keys to string. Non atom keys are unaffected.
Examples
iex> KeyConvert.stringify(%{amount: 100})
%{"amount" => 100}
iex> KeyConvert.stringify(%{100 => "amount"})
%{100 => "amount"}