jsonapi v1.0.0 JSONAPI.Utils.String View Source
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
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
camelize(value) View Source
Replace underscores or dashes between words in value
with camelCasing
Ignores underscores or dashes that are not between letters/numbers
Examples
iex> camelize("top_posts")
"topPosts"
iex> camelize(:top_posts)
"topPosts"
iex> camelize("_top_posts")
"_topPosts"
iex> camelize("_top__posts_")
"_top__posts_"
dasherize(value) View Source
Replace underscores between words in value
with dashes
Ignores underscores that are not between letters/numbers
Examples
iex> dasherize("top_posts")
"top-posts"
iex> dasherize("_top_posts")
"_top-posts"
iex> dasherize("_top__posts_")
"_top__posts_"
expand_fields(value, fun) View Source
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]}
field_transformation() View Source
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
camelCase fields:
config :jsonapi, field_transformation: :camelize
Dashed fields:
config :jsonapi, field_transformation: :dasherize
Underscored fields:
config :jsonapi, field_transformation: :underscore
underscore(value) View Source
Replace dashes between words in value
with underscores
Ignores dashes that are not between letters/numbers
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-"