CozyCase (cozy_case v0.3.1)
View SourceConverts 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
| name | example |
|---|---|
| snake case | welcome_message |
| kebab case | welcome-message |
| camel case | welcomeMessage |
| pascal case | WelcomeMessage |
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.
With 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
With 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.