ConvCase (hl7 v1.0.0)
Functions to convert strings, atoms and map keys between camelCase
,
snake_case
and kebab-case
.
Currently this functions do not support UTF-8.
If this package fits not your requirements then take a look here:
- Macro.camelize/1 and Macro.underscore/1
- ReCase helps you to convert a string from any case to any case.
- ProperCase an Elixir library that converts keys in maps between snake_case and camel_case.
Summary
Functions
Converts snake_case
and kebab-case
into camelCase
.
Converts snake_case
and camelCase
into kebab-case
.
Converts camelCase
and kebab-case
into snake_case
.
Functions
to_camel_case(value)
Converts snake_case
and kebab-case
into camelCase
.
For strings, the function returns the converted string.
Examples
iex> ConvCase.to_camel_case("foo_bar")
"fooBar"
iex> ConvCase.to_camel_case("foo-bar")
"fooBar"
For atoms, the function returns the converted atom. This function used
String.to_existing_atom/1
.
Examples
iex> ConvCase.to_camel_case(:foo_bar)
:fooBar
For lists, the function returns a list with converted values.
Examples
iex> ConvCase.to_camel_case(["foo_bar", "foo-bar"])
["fooBar", "fooBar"]
For tuples, the function returns a tuple with converted values.
Examples
iex> ConvCase.to_camel_case({"foo_bar", "foo-bar"})
{"fooBar", "fooBar"}
For maps, the function returns a map with converted keys. The type of the key
will not be changed. New atoms are generated by String.to_existing_atom/1
.
Keys of nested maps are converted too.
Examples
iex> ConvCase.to_camel_case(%{foo_bar: %{"foo-bar" => "foo-bar"}})
%{fooBar: %{"fooBar" => "foo-bar"}}
For other types, the function returns the given value.
Examples
iex> ConvCase.to_camel_case(42)
42
to_kebab_case(value)
Converts snake_case
and camelCase
into kebab-case
.
For strings, the function returns the converted string.
Examples
iex> ConvCase.to_kebab_case("foo_bar")
"foo-bar"
iex> ConvCase.to_kebab_case("fooBar")
"foo-bar"
For atoms, the function returns the converted atom. This function used
String.to_existing_atom/1
.
Examples
iex> ConvCase.to_kebab_case(:foo_bar)
:"foo-bar"
For lists, the function returns a list with converted values.
Examples
iex> ConvCase.to_kebab_case(["foo_bar", "fooBar"])
["foo-bar", "foo-bar"]
For tuples, the function returns a tuple with converted values.
Examples
iex> ConvCase.to_kebab_case({"foo_bar", "fooBar"})
{"foo-bar", "foo-bar"}
For maps, the function returns a map with converted keys. The type of the key
will not be changed. New atoms are generated by String.to_existing_atom/1
.
Keys of nested maps are converted too.
Examples
iex> ConvCase.to_kebab_case(%{foo_bar: %{"fooBar" => "fooBar"}})
%{"foo-bar": %{"foo-bar" => "fooBar"}}
For other types, the function returns the given value.
Examples
iex> ConvCase.to_kebab_case(42)
42
to_snake_case(string)
Converts camelCase
and kebab-case
into snake_case
.
For strings, the function returns the converted string.
Examples
iex> ConvCase.to_snake_case("fooBar")
"foo_bar"
iex> ConvCase.to_snake_case("foo-bar")
"foo_bar"
For atoms, the function returns the converted atom.
String.to_existing_atom/1
is used to create "new" atoms.
iex> ConvCase.to_snake_case(:fooBar)
:foo_bar
For lists, the function returns a list with converted values.
iex> ConvCase.to_snake_case(["fooBar", "foo-bar"])
["foo_bar", "foo_bar"]
For tuples, the function returns a tuple with converted values.
iex> ConvCase.to_snake_case({"fooBar", "foo-bar"})
{"foo_bar", "foo_bar"}
For maps, the function returns a map with converted keys. The type of the key
will not be changed. New atoms are generated by String.to_existing_atom/1
.
The keys of nested maps are also converted.
iex> ConvCase.to_snake_case(%{fooBar: %{"foo-bar" => "foo-bar"}})
%{foo_bar: %{"foo_bar" => "foo-bar"}}
For other types, the function returns the given value.
iex> ConvCase.to_snake_case(42)
42