View Source CozyCase (cozy_case v0.1.0)

Convert data between common naming conventions, such as snake case, kebab case, camel case and pascal case.

Currently, this module provides these main functions:

common-naming-conventions

Common naming conventions

nameexample
snake casewelcome_message
kebab casewelcome-message
camel casewelcomeMessage
pascal caseWelcomeMessage

Read Naming convention - Examples of multiple-word identifier formats for more multiple-word identifier formats.

examples

Examples

All these functions have the same intefaces.

For strings or atoms, these functions convert them directly:

iex> CozyCase.snake_case("HelloWorld")
"hello_world"

iex> CozyCase.snake_case(HelloWorld)
"hello_world"

For maps, there functions convert the keys of maps recursively, without touching the values of maps:

iex> CozyCase.snake_case(%{
iex>   "FamilyMembers" => [
iex>     %{
iex>       "Name" => "Lily",
iex>       "Age" => 50,
iex>       "Hobbies" => ["Dreaming", "Singing"]
iex>     },
iex>     %{
iex>       "Name" => "Charlie",
iex>       "Age" => 55,
iex>       "Hobbies" => ["Dreaming", "Singing"]
iex>     }
iex>   ]
iex> })
%{
  "family_members" => [
    %{"name" => "Lily", "age" => 50, "hobbies" => ["Dreaming", "Singing"]},
    %{"name" => "Charlie", "age" => 55, "hobbies" => ["Dreaming", "Singing"]}
  ]
}

For lists, these functions convert the keys of maps in lists recursively:

iex> CozyCase.snake_case([
iex>     %{
iex>       "Name" => "Lily",
iex>       "Age" => 50,
iex>       "Hobbies" => ["Dreaming", "Singing"]
iex>     },
iex>     %{
iex>       "Name" => "Charlie",
iex>       "Age" => 55,
iex>       "Hobbies" => ["Dreaming", "Singing"]
iex>     }
iex>   ])
[
  %{"name" => "Lily", "age" => 50, "hobbies" => ["Dreaming", "Singing"]},
  %{"name" => "Charlie", "age" => 55, "hobbies" => ["Dreaming", "Singing"]}
]

integrate-with-other-packages

Integrate with other packages

CozyCase doesn't provide higher-level wrappers. Because everything is a function, it's very easy to integrate CozyCase with other packages.

plug

plug

Converts params in %Plug.Conn{} to snake case:

defmodule DemoWeb.Plug.SnakeCaseParams do
  @behaviour Plug

  @impl true
  def init(opts), do: opts

  @impl true
  def call(%{params: params} = conn, _opts) do
    %{conn | params: CozyCase.snake_case(params)}
  end
end

jason

jason

Decode a JSON string:

iex> json = "{\"Age\":23,\"FamilyMembers\":[],\"Name\":\"Lenna\"}"
iex> Jason.decode!(json, keys: &CozyCase.snake_case/1)
%{"name" => "Lenna", "age" => 23, "family_members" => []}

Encode a map as a JSON string:

iex> map = %{"name" => "Lenna", "age" => 23, "family_members" => []}
iex> map |> CozyCase.camel_case() |> Jason.encode!()
"{\"age\":23,\"familyMembers\":[],\"name\":\"Lenna\"}"

Link to this section Summary

Link to this section Types

@type accepted_data_types() :: String.t() | atom() | map() | list()

Link to this section Functions

@spec camel_case(accepted_data_types()) :: String.t()
@spec kebab_case(accepted_data_types()) :: String.t()
@spec pascal_case(accepted_data_types()) :: String.t()
@spec snake_case(accepted_data_types()) :: String.t()