Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 10 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ iex> CurrencyFormatter.format(654300, :eur)
iex> CurrencyFormatter.format(654300, :eur, keep_decimals: true)
"€6.543,00"

iex> CurrencyFormatter.format(1, "huf")
"0,01Ft"

iex> CurrencyFormatter.format(123456)
"$1,234.56"
Expand All @@ -34,39 +36,19 @@ iex> CurrencyFormatter.format(654321, "AUD", disambiguate: true)

Formatting cents to a currency raw html string
```elixir
iex> CurrencyFormatter.raw_html_format(123456, "EUR")
~s[<span class="currency-formatter-symbol">€</span><span class="currency-formatter-amount">1.234,56</span>]
"""
iex> CurrencyFormatter.raw_html_format(123456, "EUR")
~s[<span class="currency-formatter-symbol">€</span><span class="currency-formatter-amount">1.234,56</span>]
```

Formatting cents to a currency safe(phoenix) html string

```elixir
iex> CurrencyFormatter.html_format(123456, "EUR")
[
safe: [
60,
"span",
[[32, "class", 61, 34, "currency-formatter-symbol", 34]],
62,
"€",
60,
47,
"span",
62
],
safe: [
60,
"span",
[[32, "class", 61, 34, "currency-formatter-amount", 34]],
62,
"1.234,56",
60,
47,
"span",
62
]
]
{:safe,
[60, "span", 32, "class", 61, 34, "currency-formatter-symbol", 34,
62, "€", 60, 47, "span", 62, 60, "span", 32, "class", 61, 34,
"currency-formatter-amount", 34, 62, "1.234,56", 60, 47, "span",
62]}
```

Requesting formatting instructions for a currency
Expand Down Expand Up @@ -126,7 +108,7 @@ As this is [available in Hex](https://hex.pm/docs/publish), the package can be i

```elixir
def deps do
[{:currency_formatter, "~> 0.4"}]
[{:currency_formatter, "~> 0.9.0"}]
end
```

Expand Down
41 changes: 13 additions & 28 deletions lib/currency_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule CurrencyFormatter do
|> split_units_and_subunits
|> handle_cents(format, opts)
|> set_symbol(format, opts)
|> flatten()
end

@doc """
Expand All @@ -58,30 +59,11 @@ defmodule CurrencyFormatter do
## example

iex> CurrencyFormatter.html_format(123456, "EUR")
[
safe: [
60,
"span",
[[32, "class", 61, 34, "currency-formatter-symbol", 34]],
62,
"€",
60,
47,
"span",
62
],
safe: [
60,
"span",
[[32, "class", 61, 34, "currency-formatter-amount", 34]],
62,
"1.234,56",
60,
47,
"span",
62
]
]
{:safe,
[60, "span", 32, "class", 61, 34, "currency-formatter-symbol", 34,
62, "€", 60, 47, "span", 62, 60, "span", 32, "class", 61, 34,
"currency-formatter-amount", 34, 62, "1.234,56", 60, 47, "span",
62]}
"""
def html_format(number, currency, opts \\ Keyword.new()) do
opts = Keyword.put_new(opts, :html, true)
Expand All @@ -103,8 +85,7 @@ defmodule CurrencyFormatter do
def raw_html_format(number, currency, opts \\ Keyword.new()) do
number
|> html_format(currency, opts)
|> Enum.map(&Phoenix.HTML.safe_to_string/1)
|> Enum.join("")
|> Phoenix.HTML.safe_to_string()
end

@doc """
Expand Down Expand Up @@ -336,8 +317,7 @@ defmodule CurrencyFormatter do

defp set_symbol(number_string, config, opts) do
if Keyword.get(opts, :html) do
spans = wrap_in_spans(amount: number_string, symbol: get_symbol(config, opts))
Phoenix.HTML.safe_to_string(spans)
wrap_in_spans(amount: number_string, symbol: get_symbol(config, opts))
else
number_string <> get_symbol(config, opts)
end
Expand Down Expand Up @@ -365,4 +345,9 @@ defmodule CurrencyFormatter do
@spec get_disambiguous_symbol(map) :: String.t()
defp get_disambiguous_symbol(%{"disambiguate_symbol" => symbol}), do: symbol
defp get_disambiguous_symbol(config), do: config["symbol"]

defp flatten(list) when is_list(list),
do: {:safe, list |> Enum.map(&(elem(&1, 1))) |> List.flatten()}

defp flatten(data), do: data
end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CurrencyFormatter.Mixfile do
def project do
[
app: :currency_formatter,
version: "0.8.1",
version: "0.9.0",
description: "A library to help with formatting a number to a currency using iso standards and other convenience functions related to formatting currencies",
package: package(),
elixir: "~> 1.7",
Expand Down Expand Up @@ -43,7 +43,7 @@ defmodule CurrencyFormatter.Mixfile do
{:earmark, ">= 0.0.0", only: :dev},
{:ex_doc, ">= 0.0.0", only: :dev},
{:excoveralls, ">= 0.0.0", only: :test},
{:phoenix_html, ">= 0.0.0"},
{:phoenix_html, ">= 2.12.0"},
{:poison, "~> 3.1.0"},
]
end
Expand Down
5 changes: 5 additions & 0 deletions test/currency_formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,9 @@ defmodule CurrencyFormatterTest do
assert Map.get(currencies, String.downcase(code)) == nil
end)
end

test "can convert to html string" do
assert "<span class=\"currency-formatter-symbol\">€</span><span class=\"currency-formatter-amount\">0,01</span>" == CurrencyFormatter.html_format(1, :eur) |> Phoenix.HTML.safe_to_string()
assert "<span class=\"currency-formatter-amount\">1</span><span class=\"currency-formatter-symbol\">د.إ</span>" == CurrencyFormatter.format("1.00", "AED", html: true) |> Phoenix.HTML.safe_to_string()
end
end