View Source JSONAPI.Utils.String (jsonapi v1.6.2)
String manipulation helpers.
Link to this section Summary
Functions
Replace underscores or dashes between words in value
with camelCasing
Replace underscores between words in value
with dashes
Examples
iex> expand_fields(%{"foo-bar" => "baz"}, &underscore/1)
%{"foo_bar" => "baz"}
iex> expand_fields(%{"foo_bar" => "baz"}, &dasherize/1)
%{"foo-bar" => "baz"}
iex> expand_fields(%{"foo-bar" => "baz"}, &camelize/1)
%{"fooBar" => "baz"}
iex> expand_fields({"foo-bar", "dollar-sol"}, &underscore/1)
{"foo_bar", "dollar-sol"}
iex> expand_fields({"foo-bar", %{"a-d" => "z-8"}}, &underscore/1)
{"foo_bar", %{"a_d" => "z-8"}}
iex> expand_fields(%{"f-b" => %{"a-d" => "z"}, "c-d" => "e"}, &underscore/1)
%{"f_b" => %{"a_d" => "z"}, "c_d" => "e"}
iex> expand_fields(%{"f-b" => %{"a-d" => %{"z-w" => "z"}}, "c-d" => "e"}, &underscore/1)
%{"f_b" => %{"a_d" => %{"z_w" => "z"}}, "c_d" => "e"}
iex> expand_fields(:"foo-bar", &underscore/1)
"foo_bar"
iex> expand_fields(:foo_bar, &dasherize/1)
"foo-bar"
iex> expand_fields(:"foo-bar", &camelize/1)
"fooBar"
iex> expand_fields(%{"f-b" => "a-d"}, &underscore/1)
%{"f_b" => "a-d"}
iex> expand_fields(%{"inserted-at" => ~N[2019-01-17 03:27:24.776957]}, &underscore/1)
%{"inserted_at" => ~N[2019-01-17 03:27:24.776957]}
iex> expand_fields(%{"xValue" => 123}, &underscore/1)
%{"x_value" => 123}
iex> expand_fields(%{"attributes" => %{"corgiName" => "Wardel"}}, &underscore/1)
%{"attributes" => %{"corgi_name" => "Wardel"}}
iex> expand_fields(%{"attributes" => %{"corgiName" => ["Wardel"]}}, &underscore/1)
%{"attributes" => %{"corgi_name" => ["Wardel"]}}
iex> expand_fields(%{"attributes" => %{"someField" => ["SomeValue", %{"nestedField" => "Value"}]}}, &underscore/1)
%{"attributes" => %{"some_field" => ["SomeValue", %{"nested_field" => "Value"}]}}
iex> expand_fields([%{"fooBar" => "a"}, %{"fooBar" => "b"}], &underscore/1)
[%{"foo_bar" => "a"}, %{"foo_bar" => "b"}]
iex> expand_fields([%{"foo_bar" => "a"}, %{"foo_bar" => "b"}], &camelize/1)
[%{"fooBar" => "a"}, %{"fooBar" => "b"}]
iex> expand_fields(%{"fooAttributes" => [%{"fooBar" => "a"}, %{"fooBar" => "b"}]}, &underscore/1)
%{"foo_attributes" => [%{"foo_bar" => "a"}, %{"foo_bar" => "b"}]}
iex> expand_fields(%{"foo_attributes" => [%{"foo_bar" => "a"}, %{"foo_bar" => "b"}]}, &camelize/1)
%{"fooAttributes" => [%{"fooBar" => "a"}, %{"fooBar" => "b"}]}
iex> expand_fields(%{"foo_attributes" => [%{"foo_bar" => [1, 2]}]}, &camelize/1)
%{"fooAttributes" => [%{"fooBar" => [1, 2]}]}
The configured transformation for the API's fields. JSON:API v1.1 recommends using camlized fields (e.g. "goodDog", versus "good_dog"). However, we don't hold a strong opinion, so feel free to customize it how you would like (e.g. "good-dog", versus "good_dog").
Replace dashes between words in value
with underscores
Link to this section Functions
Replace underscores or dashes between words in value
with camelCasing
Ignores underscores or dashes that are not between letters/numbers
examples
Examples
iex> camelize("top_posts")
"topPosts"
iex> camelize(:top_posts)
"topPosts"
iex> camelize("_top_posts")
"_topPosts"
iex> camelize("_top__posts_")
"_top__posts_"
iex> camelize("")
""
iex> camelize("alreadyCamelized")
"alreadyCamelized"
Replace underscores between words in value
with dashes
Ignores underscores that are not between letters/numbers
examples
Examples
iex> dasherize("top_posts")
"top-posts"
iex> dasherize("_top_posts")
"_top-posts"
iex> dasherize("_top__posts_")
"_top__posts_"
@spec expand_fields(map(), function()) :: map()
@spec expand_fields(list(), function()) :: list()
@spec expand_fields(tuple(), function()) :: tuple()
@spec expand_fields(String.t() | atom(), function()) :: String.t()
examples
Examples
iex> expand_fields(%{"foo-bar" => "baz"}, &underscore/1)
%{"foo_bar" => "baz"}
iex> expand_fields(%{"foo_bar" => "baz"}, &dasherize/1)
%{"foo-bar" => "baz"}
iex> expand_fields(%{"foo-bar" => "baz"}, &camelize/1)
%{"fooBar" => "baz"}
iex> expand_fields({"foo-bar", "dollar-sol"}, &underscore/1)
{"foo_bar", "dollar-sol"}
iex> expand_fields({"foo-bar", %{"a-d" => "z-8"}}, &underscore/1)
{"foo_bar", %{"a_d" => "z-8"}}
iex> expand_fields(%{"f-b" => %{"a-d" => "z"}, "c-d" => "e"}, &underscore/1)
%{"f_b" => %{"a_d" => "z"}, "c_d" => "e"}
iex> expand_fields(%{"f-b" => %{"a-d" => %{"z-w" => "z"}}, "c-d" => "e"}, &underscore/1)
%{"f_b" => %{"a_d" => %{"z_w" => "z"}}, "c_d" => "e"}
iex> expand_fields(:"foo-bar", &underscore/1)
"foo_bar"
iex> expand_fields(:foo_bar, &dasherize/1)
"foo-bar"
iex> expand_fields(:"foo-bar", &camelize/1)
"fooBar"
iex> expand_fields(%{"f-b" => "a-d"}, &underscore/1)
%{"f_b" => "a-d"}
iex> expand_fields(%{"inserted-at" => ~N[2019-01-17 03:27:24.776957]}, &underscore/1)
%{"inserted_at" => ~N[2019-01-17 03:27:24.776957]}
iex> expand_fields(%{"xValue" => 123}, &underscore/1)
%{"x_value" => 123}
iex> expand_fields(%{"attributes" => %{"corgiName" => "Wardel"}}, &underscore/1)
%{"attributes" => %{"corgi_name" => "Wardel"}}
iex> expand_fields(%{"attributes" => %{"corgiName" => ["Wardel"]}}, &underscore/1)
%{"attributes" => %{"corgi_name" => ["Wardel"]}}
iex> expand_fields(%{"attributes" => %{"someField" => ["SomeValue", %{"nestedField" => "Value"}]}}, &underscore/1)
%{"attributes" => %{"some_field" => ["SomeValue", %{"nested_field" => "Value"}]}}
iex> expand_fields([%{"fooBar" => "a"}, %{"fooBar" => "b"}], &underscore/1)
[%{"foo_bar" => "a"}, %{"foo_bar" => "b"}]
iex> expand_fields([%{"foo_bar" => "a"}, %{"foo_bar" => "b"}], &camelize/1)
[%{"fooBar" => "a"}, %{"fooBar" => "b"}]
iex> expand_fields(%{"fooAttributes" => [%{"fooBar" => "a"}, %{"fooBar" => "b"}]}, &underscore/1)
%{"foo_attributes" => [%{"foo_bar" => "a"}, %{"foo_bar" => "b"}]}
iex> expand_fields(%{"foo_attributes" => [%{"foo_bar" => "a"}, %{"foo_bar" => "b"}]}, &camelize/1)
%{"fooAttributes" => [%{"fooBar" => "a"}, %{"fooBar" => "b"}]}
iex> expand_fields(%{"foo_attributes" => [%{"foo_bar" => [1, 2]}]}, &camelize/1)
%{"fooAttributes" => [%{"fooBar" => [1, 2]}]}
The configured transformation for the API's fields. JSON:API v1.1 recommends using camlized fields (e.g. "goodDog", versus "good_dog"). However, we don't hold a strong opinion, so feel free to customize it how you would like (e.g. "good-dog", versus "good_dog").
This library currently supports camelized, dashed and underscored fields.
configuration-examples
Configuration examples
camelCase fields:
config :jsonapi, field_transformation: :camelize
Dashed fields:
config :jsonapi, field_transformation: :dasherize
Underscored fields:
config :jsonapi, field_transformation: :underscore
Replace dashes between words in value
with underscores
Ignores dashes that are not between letters/numbers
examples
Examples
iex> underscore("top-posts")
"top_posts"
iex> underscore(:top_posts)
"top_posts"
iex> underscore("-top-posts")
"-top_posts"
iex> underscore("-top--posts-")
"-top--posts-"
iex> underscore("corgiAge")
"corgi_age"