Skip to content

Commit

Permalink
Add Cldr.Currency.current_territory_currencies/1
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jan 27, 2023
1 parent 1f31cda commit 3471453
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Cldr_Currencies v2.15.0

This is the changelog for Cldr_Currencies v2.15.0 released on January 28th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_currencies/tags)

### Enhancements

* Add `Cldr.Currency.current_territory_currencies/1` and `MyApp.Cldr.Currency.current_territory_currencies/0` that returns a mapping from territory code from currency type for all territories that have a current currency. Thanks for @lawik for the inspiration.

## Cldr_Currencies v2.14.3

This is the changelog for Cldr_Currencies v2.14.3 released on January 27th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_currencies/tags)
Expand Down
24 changes: 24 additions & 0 deletions lib/cldr/backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,30 @@ defmodule Cldr.Currency.Backend do
{:error, Cldr.Locale.locale_error(locale)}
end

@doc """
Returns a mapping from a territory code to its
current currency code.
If a territory has no current currency (like
Antartica, territory code `:AQ`) then no
mapping is returned for that territory.
## Returns
* A map of `{territory_code => Cldr.Currency.t}`
## Example
iex> #{inspect __MODULE__}.current_territory_currencies()
"""
@doc since: "2.15.0"

@spec current_territory_currencies() :: %{Cldr.Locale.territory_code() => Cldr.Currency.t()}
def current_territory_currencies do
Cldr.Currency.current_territory_currencies(unquote(backend))
end

@doc """
Returns a map of the metadata for all currencies for
a given locale and raises on error.
Expand Down
56 changes: 51 additions & 5 deletions lib/cldr/currency.ex
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,47 @@ defmodule Cldr.Currency do
end
end

@doc """
Returns a mapping from a territory code to its
current currency code.
If a territory has no current currency (like
Antartica, territory code `:AQ`) then no
mapping is returned for that territory.
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is `Cldr.default_backend!/0`
## Returns
* A map of `{territory_code => Cldr.Currency.t}`
"""
@doc since: "2.15.0"

@spec current_territory_currencies() :: %{Cldr.Locale.territory_code() => t()}
def current_territory_currencies(backend \\ Cldr.default_backend!()) do
territory_currencies()
|> Enum.map(fn
{:ZZ, _currencies} ->
nil

{territory, _currencies} ->
case current_currency_for_territory(territory) do
nil -> {territory, nil}
currency -> {territory, currency_for_code!(currency, backend)}
end
end)
|> Enum.reject(fn
{_territory, nil} -> true
nil -> true
_other -> false
end)
|> Map.new()
end

@doc """
Returns a list of historic and the current
currency for a given locale.
Expand Down Expand Up @@ -882,9 +923,10 @@ defmodule Cldr.Currency do
of an ISO 4217 currency code, or a `t:Cldr.Currency` struct.
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module
is a `Cldr` backend module. The default is `Cldr.default_backend!/0`.
* `options` is a `Keyword` list of options.
* `options` is a `Keyword` list of options. The default is
`[]`.
## Options
Expand Down Expand Up @@ -936,13 +978,13 @@ defmodule Cldr.Currency do
@spec currency_for_code(code() | t(), Cldr.backend(), Keyword.t()) ::
{:ok, t()} | {:error, {module(), String.t()}}

def currency_for_code(currency_or_currency_code, backend, options \\ [])
def currency_for_code(currency_or_currency_code, backend \\ Cldr.default_backend!(), options \\ [])

def currency_for_code(%__MODULE__{} = currency, _backend, _options) do
{:ok, currency}
end

def currency_for_code(currency_code, backend, options) do
def currency_for_code(currency_code, backend, options) when is_atom(backend) and is_list(options) do
{locale, backend} = Cldr.locale_and_backend_from(options[:locale], backend)

with {:ok, code} <- Cldr.validate_currency(currency_code),
Expand All @@ -952,6 +994,10 @@ defmodule Cldr.Currency do
end
end

def currency_for_code(currency_code, nil, [] = options) when is_list(options) do
currency_for_code(currency_code, nil, options)
end

@doc """
Returns the currency metadata for the requested currency code.
Expand Down Expand Up @@ -1014,7 +1060,7 @@ defmodule Cldr.Currency do
@spec currency_for_code!(code() | t(), Cldr.backend(), Keyword.t()) ::
t() | no_return()

def currency_for_code!(currency_or_currency_code, backend, options \\ []) do
def currency_for_code!(currency_or_currency_code, backend \\ nil, options \\ []) do
case currency_for_code(currency_or_currency_code, backend, options) do
{:ok, currency} -> currency
{:error, {exception, reason}} -> raise exception, reason
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Cldr.Currencies.MixProject do
use Mix.Project

@version "2.14.3"
@version "2.15.0"

def project do
[
Expand Down

0 comments on commit 3471453

Please sign in to comment.