Skip to content

Commit

Permalink
Merge pull request #4 from mediapress-ltd/jv-currency-formatting-perf…
Browse files Browse the repository at this point in the history
…ormance

Allow `%Currency{}` structs in `currency_for_code/3`
  • Loading branch information
kipcole9 authored Jun 16, 2021
2 parents 6f5d0b4 + bf961d2 commit e0b3194
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
12 changes: 6 additions & 6 deletions lib/cldr/backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ defmodule Cldr.Currency.Backend do
## Arguments
* `currency_code` is a `binary` or `atom` representation of an
ISO 4217 currency code.
* `currency_or_currency_code` is a `binary` or `atom` representation
of an ISO 4217 currency code, or a `%Cldr.Currency{}` struct.
## Examples
Expand Down Expand Up @@ -288,14 +288,14 @@ defmodule Cldr.Currency.Backend do
}}
"""
@spec currency_for_code(Cldr.Currency.code, Keyword.t()) ::
{:ok, Cldr.Currency.t} | {:error, {module(), String.t()}}
@spec currency_for_code(Cldr.Currency.code() | Cldr.Currency.t(), Keyword.t()) ::
{:ok, Cldr.Currency.t()} | {:error, {module(), String.t()}}

def currency_for_code(
currency_code,
currency_or_currency_code,
options \\ [locale: unquote(backend).default_locale()]
) do
Cldr.Currency.currency_for_code(currency_code, unquote(backend), options)
Cldr.Currency.currency_for_code(currency_or_currency_code, unquote(backend), options)
end

defp get_currency_metadata(code, nil) do
Expand Down
14 changes: 8 additions & 6 deletions lib/cldr/currency.ex
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,8 @@ defmodule Cldr.Currency do
## Arguments
* `currency_code` is a `binary` or `atom` representation of an
ISO 4217 currency code.
* `currency_or_currency_code` is a `binary` or `atom` representation
of an ISO 4217 currency code, or a `%Cldr.Currency{}` struct.
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module
Expand Down Expand Up @@ -820,16 +820,18 @@ defmodule Cldr.Currency do
"""

@spec currency_for_code(code, Cldr.backend(), Keyword.t()) ::
{:ok, t} | {:error, {module(), String.t()}}
@spec currency_for_code(code() | t(), Cldr.backend(), Keyword.t()) ::
{:ok, t()} | {:error, {module(), String.t()}}

def currency_for_code(currency_code, backend, options \\ []) do
def currency_for_code(currency_or_currency_code, backend, options \\ [])
def currency_for_code(%__MODULE__{} = currency, _backend, _options), do: {:ok, currency}

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

with {:ok, code} <- known_currency_code(currency_code),
{:ok, locale} <- Cldr.validate_locale(locale, backend),
{:ok, currencies} <- currencies_for_locale(locale, backend) do

{:ok, Map.get_lazy(currencies, code, fn -> Map.get(private_currencies(), code) end)}
end
end
Expand Down

0 comments on commit e0b3194

Please sign in to comment.