View Source Phoenix.Naming (Phoenix v1.7.1)

Conveniences for inflecting and working with names in Phoenix.

Link to this section Summary

Functions

Converts a string to camel case.

Converts an attribute/form field into its humanize version.

Extracts the resource name from an alias.

Converts a string to underscore case.

Removes the given suffix from the name if it exists.

Link to this section Functions

@spec camelize(String.t()) :: String.t()

Converts a string to camel case.

Takes an optional :lower flag to return lowerCamelCase.

examples

Examples

iex> Phoenix.Naming.camelize("my_app")
"MyApp"

iex> Phoenix.Naming.camelize("my_app", :lower)
"myApp"

In general, camelize can be thought of as the reverse of underscore, however, in some cases formatting may be lost:

Phoenix.Naming.underscore "SAPExample"  #=> "sap_example"
Phoenix.Naming.camelize   "sap_example" #=> "SapExample"
@spec camelize(String.t(), :lower) :: String.t()
@spec humanize(atom() | String.t()) :: String.t()

Converts an attribute/form field into its humanize version.

examples

Examples

iex> Phoenix.Naming.humanize(:username)
"Username"
iex> Phoenix.Naming.humanize(:created_at)
"Created at"
iex> Phoenix.Naming.humanize("user_id")
"User"
Link to this function

resource_name(alias, suffix \\ "")

View Source
@spec resource_name(String.Chars.t(), String.t()) :: String.t()

Extracts the resource name from an alias.

examples

Examples

iex> Phoenix.Naming.resource_name(MyApp.User)
"user"

iex> Phoenix.Naming.resource_name(MyApp.UserView, "View")
"user"
@spec underscore(String.t()) :: String.t()

Converts a string to underscore case.

examples

Examples

iex> Phoenix.Naming.underscore("MyApp")
"my_app"

In general, underscore can be thought of as the reverse of camelize, however, in some cases formatting may be lost:

Phoenix.Naming.underscore "SAPExample"  #=> "sap_example"
Phoenix.Naming.camelize   "sap_example" #=> "SapExample"
@spec unsuffix(String.t(), String.t()) :: String.t()

Removes the given suffix from the name if it exists.

examples

Examples

iex> Phoenix.Naming.unsuffix("MyApp.User", "View")
"MyApp.User"

iex> Phoenix.Naming.unsuffix("MyApp.UserView", "View")
"MyApp.User"