Skip to content

Commit

Permalink
type for locale
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegillet committed Sep 25, 2023
1 parent 4fd44bf commit e90b597
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
15 changes: 8 additions & 7 deletions exercises/practice/ledger/.meta/example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ defmodule Ledger do
Format the given entries given a currency and locale
"""
@type currency :: :usd | :eur
@type locale :: :en_US | :nl_NL
@type entry :: %{amount_in_cents: integer(), date: Date.t(), description: String.t()}

@spec format_entries(currency(), String.t(), list(entry())) :: String.t()
@spec format_entries(currency(), locale(), list(entry())) :: String.t()
def format_entries(currency, locale, entries) do
header = header(locale)

Expand All @@ -17,8 +18,8 @@ defmodule Ledger do
Enum.join([header | entries], "\n") <> "\n"
end

defp header("en_US"), do: "Date | Description | Change "
defp header("nl_NL"), do: "Datum | Omschrijving | Verandering "
defp header(:en_US), do: "Date | Description | Change "
defp header(:nl_NL), do: "Datum | Omschrijving | Verandering "

defp compare_entries(a, b) do
case Date.compare(a.date, b.date) do
Expand Down Expand Up @@ -61,21 +62,21 @@ defmodule Ledger do
Enum.join([date, description, amount], " | ")
end

defp format_date(date, "en_US") do
defp format_date(date, :en_US) do
year = date.year
month = date.month |> to_string() |> String.pad_leading(2, "0")
day = date.day |> to_string() |> String.pad_leading(2, "0")
Enum.join([month, day, year], "/")
end

defp format_date(date, "nl_NL") do
defp format_date(date, :nl_NL) do
year = date.year
month = date.month |> to_string() |> String.pad_leading(2, "0")
day = date.day |> to_string() |> String.pad_leading(2, "0")
Enum.join([day, month, year], "-")
end

defp format_amount(amount, currency, "en_US") do
defp format_amount(amount, currency, :en_US) do
currency = format_currency(currency)
number = format_number(abs(amount), ".", ",")

Expand All @@ -86,7 +87,7 @@ defmodule Ledger do
end
end

defp format_amount(amount, currency, "nl_NL") do
defp format_amount(amount, currency, :nl_NL) do
currency = format_currency(currency)
number = format_number(abs(amount), ",", ".")

Expand Down
13 changes: 7 additions & 6 deletions exercises/practice/ledger/lib/ledger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ defmodule Ledger do
Format the given entries given a currency and locale
"""
@type currency :: :usd | :eur
@type locale :: :en_US | :nl_NL
@type entry :: %{amount_in_cents: integer(), date: Date.t(), description: String.t()}

@spec format_entries(currency(), String.t(), list(entry())) :: String.t()
@spec format_entries(currency(), locale(), list(entry())) :: String.t()
def format_entries(currency, locale, entries) do
header =
if locale == "en_US" do
if locale == :en_US do
"Date | Description | Change \n"
else
"Datum | Omschrijving | Verandering \n"
Expand Down Expand Up @@ -40,14 +41,14 @@ defmodule Ledger do
day = entry.date.day |> to_string() |> String.pad_leading(2, "0")

date =
if locale == "en_US" do
if locale == :en_US do
month <> "/" <> day <> "/" <> year <> " "
else
day <> "-" <> month <> "-" <> year <> " "
end

number =
if locale == "en_US" do
if locale == :en_US do
decimal =
entry.amount_in_cents |> abs |> rem(100) |> to_string() |> String.pad_leading(2, "0")

Expand Down Expand Up @@ -77,13 +78,13 @@ defmodule Ledger do

amount =
if entry.amount_in_cents >= 0 do
if locale == "en_US" do
if locale == :en_US do
" #{if(currency == :eur, do: "€", else: "$")}#{number} "
else
" #{if(currency == :eur, do: "€", else: "$")} #{number} "
end
else
if locale == "en_US" do
if locale == :en_US do
" (#{if(currency == :eur, do: "€", else: "$")}#{number})"
else
" #{if(currency == :eur, do: "€", else: "$")} -#{number} "
Expand Down
22 changes: 11 additions & 11 deletions exercises/practice/ledger/test/ledger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule LedgerTest do

# @tag :pending
test "empty ledger" do
assert Ledger.format_entries(:usd, "en_US", []) ==
assert Ledger.format_entries(:usd, :en_US, []) ==
"""
Date | Description | Change\s\s\s\s\s\s\s
"""
Expand All @@ -15,7 +15,7 @@ defmodule LedgerTest do
%{amount_in_cents: -1000, date: ~D[2015-01-01], description: "Buy present"}
]

assert Ledger.format_entries(:usd, "en_US", entries) ==
assert Ledger.format_entries(:usd, :en_US, entries) ==
"""
Date | Description | Change\s\s\s\s\s\s\s
01/01/2015 | Buy present | ($10.00)
Expand All @@ -29,7 +29,7 @@ defmodule LedgerTest do
%{amount_in_cents: -1000, date: ~D[2015-01-01], description: "Buy present"}
]

assert Ledger.format_entries(:usd, "en_US", entries) ==
assert Ledger.format_entries(:usd, :en_US, entries) ==
"""
Date | Description | Change\s\s\s\s\s\s\s
01/01/2015 | Buy present | ($10.00)
Expand All @@ -44,7 +44,7 @@ defmodule LedgerTest do
%{amount_in_cents: -1000, date: ~D[2015-01-01], description: "Buy present"}
]

assert Ledger.format_entries(:usd, "en_US", entries) ==
assert Ledger.format_entries(:usd, :en_US, entries) ==
"""
Date | Description | Change\s\s\s\s\s\s\s
01/01/2015 | Buy present | ($10.00)
Expand All @@ -60,7 +60,7 @@ defmodule LedgerTest do
%{amount_in_cents: 1, date: ~D[2015-01-01], description: "Something"}
]

assert Ledger.format_entries(:usd, "en_US", entries) ==
assert Ledger.format_entries(:usd, :en_US, entries) ==
"""
Date | Description | Change\s\s\s\s\s\s\s
01/01/2015 | Something | ($0.01)
Expand All @@ -79,7 +79,7 @@ defmodule LedgerTest do
}
]

assert Ledger.format_entries(:usd, "en_US", entries) ==
assert Ledger.format_entries(:usd, :en_US, entries) ==
"""
Date | Description | Change\s\s\s\s\s\s\s
01/01/2015 | Freude schoner Gotterf... | ($1,234.56)
Expand All @@ -92,7 +92,7 @@ defmodule LedgerTest do
%{amount_in_cents: -1000, date: ~D[2015-01-01], description: "Buy present"}
]

assert Ledger.format_entries(:eur, "en_US", entries) ==
assert Ledger.format_entries(:eur, :en_US, entries) ==
"""
Date | Description | Change\s\s\s\s\s\s\s
01/01/2015 | Buy present | (€10.00)
Expand All @@ -105,7 +105,7 @@ defmodule LedgerTest do
%{amount_in_cents: 123_456, date: ~D[2015-03-12], description: "Buy present"}
]

assert Ledger.format_entries(:usd, "nl_NL", entries) ==
assert Ledger.format_entries(:usd, :nl_NL, entries) ==
"""
Datum | Omschrijving | Verandering\s\s
12-03-2015 | Buy present | $ 1.234,56\s
Expand All @@ -118,7 +118,7 @@ defmodule LedgerTest do
%{amount_in_cents: 123_456, date: ~D[2015-03-12], description: "Buy present"}
]

assert Ledger.format_entries(:eur, "nl_NL", entries) ==
assert Ledger.format_entries(:eur, :nl_NL, entries) ==
"""
Datum | Omschrijving | Verandering\s\s
12-03-2015 | Buy present | € 1.234,56\s
Expand All @@ -131,7 +131,7 @@ defmodule LedgerTest do
%{amount_in_cents: -12345, date: ~D[2015-03-12], description: "Buy present"}
]

assert Ledger.format_entries(:usd, "nl_NL", entries) ==
assert Ledger.format_entries(:usd, :nl_NL, entries) ==
"""
Datum | Omschrijving | Verandering\s\s
12-03-2015 | Buy present | $ -123,45\s
Expand All @@ -144,7 +144,7 @@ defmodule LedgerTest do
%{amount_in_cents: -12345, date: ~D[2015-03-12], description: "Buy present"}
]

assert Ledger.format_entries(:usd, "en_US", entries) ==
assert Ledger.format_entries(:usd, :en_US, entries) ==
"""
Date | Description | Change\s\s\s\s\s\s\s
03/12/2015 | Buy present | ($123.45)
Expand Down

0 comments on commit e90b597

Please sign in to comment.