View Source CozyCase (cozy_case v0.3.0)

Converts data between common multiple-word identifier formats, such as snake case, kebab case, camel case and pascal case.

Currently, this module provides these main functions:

Multiple-word identifier formats

nameexample
snake casewelcome_message
kebab casewelcome-message
camel casewelcomeMessage
pascal caseWelcomeMessage

Read Examples of multiple-word identifier formats for more unsupported formats.

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, these functions convert the keys of maps recursively, without touching the values of maps:

iex> CozyCase.snake_case(%{
...>   "FamilyMembers" => [
...>     %{
...>       "Name" => "Lily",
...>       "Age" => 50,
...>       "Hobbies" => ["Dreaming", "Singing"]
...>     },
...>     %{
...>       "Name" => "Charlie",
...>       "Age" => 55,
...>       "Hobbies" => ["Dreaming", "Singing"]
...>     }
...>   ]
...> })
%{
  "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([
...>     %{
...>       "Name" => "Lily",
...>       "Age" => 50,
...>       "Hobbies" => ["Dreaming", "Singing"]
...>     },
...>     %{
...>       "Name" => "Charlie",
...>       "Age" => 55,
...>       "Hobbies" => ["Dreaming", "Singing"]
...>     }
...>   ])
[
  %{"name" => "Lily", "age" => 50, "hobbies" => ["Dreaming", "Singing"]},
  %{"name" => "Charlie", "age" => 55, "hobbies" => ["Dreaming", "Singing"]}
]

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

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

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\"}"

Note

CozyCase is created for the convertion between common multiple-word identifier formats.

Because of that, CozyCase doesn't handle spaces which are not used as part of indentifier.

As the user of CozyCase, you should NOT pass string containing spaces, or the result will be surprising.

iex> CozyCase.snake_case("welCome Message")
"wel_come message"

iex> CozyCase.kebab_case("wel_Come Me_ssage")
"wel-come me-ssage"

Summary

Functions

Converts other supported cases to camel case.

Converts other supported cases to kebab case.

Converts other supported cases to pascal case.

Converts other supported cases to snake case.

Functions

@spec camel_case(String.t() | atom()) :: String.t()
@spec camel_case(t) :: t when t: map() | list()

Converts other supported cases to camel case.

@spec kebab_case(String.t() | atom()) :: String.t()
@spec kebab_case(t) :: t when t: map() | list()

Converts other supported cases to kebab case.

@spec pascal_case(String.t() | atom()) :: String.t()
@spec pascal_case(t) :: t when t: map() | list()

Converts other supported cases to pascal case.

@spec snake_case(String.t() | atom()) :: String.t()
@spec snake_case(t) :: t when t: map() | list()

Converts other supported cases to snake case.