NLdoc.Util.Recase (NLdoc.Util v1.0.13)
View SourceNLdoc.Util.Recase
implements utility functions for converting the keys of maps and structs
to camelCase or snake_case.
Summary
Functions
Converts a map or struct to a map with different keys.
Converts a string or atom to camelCase and converts a map or a struct to a map with camelCase keys (recursively).
Converts a string or atom to snake_case and converts a map or a struct to a map with snake_case keys (recursively).
Types
Functions
@spec convert_keys(struct() | map(), (key() -> key()), [opt()]) :: map()
@spec convert_keys(list(), (key() -> key()), [opt()]) :: list()
@spec convert_keys(value, (key() -> key()), [opt()]) :: value when value: var
Converts a map or struct to a map with different keys.
Examples
iex> NLdoc.Util.Recase.convert_keys(
...> %{
...> HelloWorld: 1,
...> abc_123: %URI{scheme: "http", host: "example.com"},
...> fooBar: [%{Hello: 123}]
...> },
...> &Recase.to_pascal/1
...> )
%{
Abc123: %{
Authority: nil,
Fragment: nil,
Host: "example.com",
Path: nil,
Port: nil,
Query: nil,
Scheme: "http",
Userinfo: nil
},
HelloWorld: 1,
FooBar: [%{Hello: 123}]
}
@spec to_camel(struct() | map(), [opt()]) :: map()
@spec to_camel(list(), [opt()]) :: list()
@spec to_camel(key(), [opt()]) :: key()
Converts a string or atom to camelCase and converts a map or a struct to a map with camelCase keys (recursively).
Unlike Recase.to_camel/1
, this function splits the key on every dot and then calls Recase, allowing keys like
"image.source_url"
to be camelCased as "image.sourceUrl"
.
Examples
For strings and atoms:
iex> NLdoc.Util.Recase.to_camel("example_test")
"exampleTest"
iex> NLdoc.Util.Recase.to_camel(:example_test)
:exampleTest
iex> NLdoc.Util.Recase.to_camel("example_test.nested.item_one")
"exampleTest.nested.itemOne"
iex> NLdoc.Util.Recase.to_camel(:"example_test.nested.item_one")
:"exampleTest.nested.itemOne"
For maps and structs:
iex> NLdoc.Util.Recase.to_camel(
...> %{ HelloWorld: 1, abc_123: %URI{scheme: "http", host: "example.com"} }
...> )
%{
helloWorld: 1,
abc123: %{
authority: nil,
fragment: nil,
host: "example.com",
path: nil,
port: nil,
query: nil,
scheme: "http",
userinfo: nil
}
}
iex> %{my_list: [key_word: {:error, :message}, other_word: {:ok, nil}], "descriptors[0].alt_text": "test" }
...> |> NLdoc.Util.Recase.to_camel()
%{
myList: [keyWord: {:error, :message}, otherWord: {:ok, nil}],
"descriptors[0].altText": "test"
}
DateTime objects are passed through unchanged.
iex> now = DateTime.utc_now()
iex> now == now |> NLdoc.Util.Recase.to_camel()
true
@spec to_snake(struct() | map(), [opt()]) :: map()
@spec to_snake(list(), [opt()]) :: list()
@spec to_snake(key(), [opt()]) :: key()
Converts a string or atom to snake_case and converts a map or a struct to a map with snake_case keys (recursively).
Unlike Recase.to_snake/1
, this function splits the key on every dot and then calls Recase, allowing keys like
"image.sourceUrl"
to be snake_cased as "image.source_url"
.
Examples
For strings and atoms:
iex> NLdoc.Util.Recase.to_snake("exampleTest")
"example_test"
iex> NLdoc.Util.Recase.to_snake(:exampleTest)
:example_test
iex> NLdoc.Util.Recase.to_snake("exampleTest.nested.itemOne")
"example_test.nested.item_one"
iex> NLdoc.Util.Recase.to_snake(:"exampleTest.nested.itemOne")
:"example_test.nested.item_one"
For maps and structs:
iex> NLdoc.Util.Recase.to_snake(
...> %{ HelloWorld: 1, abc_123: %URI{scheme: "http", host: "example.com"} }
...> )
%{
hello_world: 1,
abc_123: %{
authority: nil,
fragment: nil,
host: "example.com",
path: nil,
port: nil,
query: nil,
scheme: "http",
userinfo: nil
}
}
iex> %{myList: [KeyWord: {:error, :message}, otherWord: {:ok, nil}], "descriptors[0].altText": "test" }
...> |> NLdoc.Util.Recase.to_snake()
%{
my_list: [key_word: {:error, :message}, other_word: {:ok, nil}],
"descriptors[0].alt_text": "test"
}
DateTime objects are passed through unchanged.
iex> now = DateTime.utc_now()
iex> now == now |> NLdoc.Util.Recase.to_snake()
true