View Source Want.String (want v2.0.0)
Manages conversions to and from strings.
Summary
Types
Functions
Casts a given value to a string.
Options
:trim- If true, strips leading and trailing whitespace before applying any other options.:max- Maximum allowable string length (character count).:min- Minimum allowable string length (character count).- ':decode' - Currently only supports :uri; runs URI.decode on the input value
:matches- The resulting string must match the given regex.
Examples
iex> Want.String.cast(1)
{:ok, "1"}
iex> Want.String.cast({:a, :b})
{:error, "Failed to convert value {:a, :b} to string."}
iex> Want.String.cast(:hello, max: 3)
{:error, "String length exceeds maximum of 3."}
iex> Want.String.cast(:a, min: 3)
{:error, "String length below minimum of 3."}
iex> Want.String.cast("hello%20world", decode: :uri)
{:ok, "hello world"}
iex> Want.String.cast(:a, matches: ~r/a/)
{:ok, "a"}
iex> Want.String.cast(" hello ", trim: true)
{:ok, "hello"}
iex> Want.String.cast(" hi ", trim: true, max: 2)
{:ok, "hi"}
Callback implementation for Want.Type.cast/2.