Packages
localize
0.41.0
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.41.3
0.41.2
0.41.1
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.1
0.30.0
retired
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
0.1.0-alpha.1
Localization (parsing, formatting) of numbers, dates/time/calendar, units of measure, messages and lists. Includes localized collation.
Current section
Files
Jump to
Current section
Files
lib/localize/list/pattern.ex
defmodule Localize.List.Pattern do
@moduledoc """
Defines the structure for list formatting patterns.
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 from
`Localize.Substitution.parse/1`.
"""
defstruct [:two, :start, :middle, :end]
@type token_list :: [String.t() | integer()]
@type t :: %__MODULE__{
two: token_list(),
start: token_list(),
middle: token_list(),
end: token_list()
}
@doc """
Creates a new pattern from a map of template strings.
### Arguments
* `options` is 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.
### Returns
* `{:ok, pattern}` where pattern is a `t:t/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]
}}
"""
@spec new(Keyword.t() | map()) :: {:ok, t()} | {:error, Exception.t()}
def new(options) when is_list(options) or is_map(options) do
with {:ok, two} <- parse_field(options, :two),
{:ok, start} <- parse_field(options, :start),
{:ok, middle} <- parse_field(options, :middle),
{:ok, end_pattern} <- parse_field(options, :end) do
{:ok, %__MODULE__{two: two, start: start, middle: middle, end: end_pattern}}
end
end
@doc """
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
* `data` is a map from locale data with keys `:start`,
`:middle`, `:end`, and `2`.
### Returns
* A `t:t/0` struct.
"""
@spec from_locale_data(map()) :: t()
def from_locale_data(data) when is_map(data) do
%__MODULE__{
two: Map.get(data, 2) || Map.get(data, :two),
start: Map.get(data, :start),
middle: Map.get(data, :middle),
end: Map.get(data, :end)
}
end
# ── Private ─────────────────────────────────────────────────
defp parse_field(options, key) do
value = access(options, key)
cond do
is_nil(value) ->
{:error,
Localize.InvalidValueError.exception(
value: nil,
expected: "a template string or parsed token list",
context: "#{key} in Localize.List.Pattern"
)}
is_binary(value) ->
{:ok, Localize.Substitution.parse(value)}
is_list(value) ->
{:ok, value}
true ->
{:error,
Localize.InvalidValueError.exception(
value: value,
expected: "a template string or parsed token list",
context: "#{key} in Localize.List.Pattern"
)}
end
end
defp access(options, key) when is_map(options), do: Map.get(options, key)
defp access(options, key) when is_list(options), do: Keyword.get(options, key)
end