Defines the structure for list formatting patterns.
Custom patterns of this type may be passed to
Localize.List.to_string/2 via the :list_style option, so the
struct is part of the documented API.
Each pattern has four components used for different list sizes:
:two— used when the list has exactly two elements.:start— used for the first pair of elements in lists with three or more elements.:middle— used for pairs in the middle of lists with four or more elements.:end— used for the last pair of elements in lists with three or more elements.
Each component is a pre-parsed token list.
Summary
Types
@type t() :: %Localize.List.Pattern{ end: token_list(), middle: token_list(), start: token_list(), two: token_list() }
Functions
Creates a pattern from locale data.
Locale data uses integer key 2 for the two-element pattern.
This function normalizes that into the standard struct form.
Arguments
datais a map from locale data with keys:start,:middle,:end, and2.
Returns
- A
t/0struct.
Examples
iex> Localize.List.Pattern.from_locale_data(%{
...> 2 => [0, " and ", 1],
...> start: [0, ", ", 1],
...> middle: [0, ", ", 1],
...> end: [0, ", and ", 1]
...> })
%Localize.List.Pattern{
two: [0, " and ", 1],
start: [0, ", ", 1],
middle: [0, ", ", 1],
end: [0, ", and ", 1]
}
@spec new(Keyword.t() | map()) :: {:ok, t()} | {:error, Exception.t()}
Creates a new pattern from a map of template strings.
Arguments
optionsis a keyword list or map with the keys:two,:start,:middle, and:end. Each value is either a template string like"{0}, and {1}"or a pre-parsed token list.
Options
:twois the pattern used when the list has exactly two elements. Required.:startis the pattern used for the first pair of elements in lists with three or more elements. Required.:middleis the pattern used for pairs in the middle of lists with four or more elements. Required.:endis the pattern used for the last pair of elements in lists with three or more elements. Required.
Returns
{:ok, pattern}where pattern is at/0.{:error, reason}if any required key is missing.
Examples
iex> Localize.List.Pattern.new(
...> two: "{0} and {1}",
...> start: "{0}, {1}",
...> middle: "{0}, {1}",
...> end: "{0}, and {1}"
...> )
{:ok,
%Localize.List.Pattern{
two: [0, " and ", 1],
start: [0, ", ", 1],
middle: [0, ", ", 1],
end: [0, ", and ", 1]
}}