Skip to content

Commit 6af632a

Browse files
committed
Add Unicode whitespace to money parser
1 parent 5698e20 commit 6af632a

5 files changed

Lines changed: 38 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This is the changelog for Money v5.3.0 released on September 5th, 2020. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)
44

5+
### Bug Fixes
6+
7+
* Fix parsing money amounts to use Unicode definition of whitespace (set `[:Zs:]`). Thanks to @Sanjibukai for the report.
8+
59
### Enhancements
610

711
* Add `Money.sum/2` to sum a list of `Money`, converting them if required.
@@ -365,28 +369,27 @@ This is the changelog for Money v3.2.2 released on February 10th, 2019. For old
365369

366370
* Improves parsing of money strings. Parsing now uses various strings that [CLDR](https://cldr.unicode.org) knows about. Some examples:
367371

368-
```
369-
iex> Money.parse "$au 12 346", locale: "fr"
370-
#Money<:AUD, 12346>
371-
iex> Money.parse "12 346 dollar australien", locale: "fr"
372-
#Money<:AUD, 12346>
373-
iex> Money.parse "A$ 12346", locale: "en"
374-
#Money<:AUD, 12346>
375-
iex> Money.parse "australian dollar 12346.45", locale: "en"
376-
#Money<:AUD, 12346.45>
377-
iex> Money.parse "AU$ 12346,45", locale: "de"
378-
#Money<:AUD, 12346.45>
379-
380-
# Can also return the strings available for a given currency
381-
# and locale
382-
iex> Cldr.Currency.strings_for_currency :AUD, "de"
383-
["aud", "au$", "australischer dollar", "australische dollar"]
384-
385-
# Round trip formatting also seems to be ok
386-
iex> {:ok, string} = Cldr.Number.to_string 1234, Money.Cldr, currency: :AUD
387-
{:ok, "A$1,234.00"}
388-
iex> Money.parse string
389-
#Money<:AUD, 1234.00>
372+
```elixir
373+
iex> Money.parse "$au 12 346", locale: "fr"
374+
#Money<:AUD, 12346>
375+
iex> Money.parse "12 346 dollar australien", locale: "fr"
376+
#Money<:AUD, 12346>
377+
iex> Money.parse "A$ 12346", locale: "en"
378+
#Money<:AUD, 12346>
379+
iex> Money.parse "australian dollar 12346.45", locale: "en"
380+
#Money<:AUD, 12346.45>
381+
iex> Money.parse "AU$ 12346,45", locale: "de"
382+
#Money<:AUD, 12346.45>
383+
384+
# Can also return the strings available for a given currency
385+
# and locale
386+
iex> Cldr.Currency.strings_for_currency :AUD, "de"
387+
["aud", "au$", "australischer dollar", "australische dollar"]
388+
389+
# Round trip formatting also seems to be ok
390+
iex> {:ok, string} = Cldr.Number.to_string 1234, Money.Cldr, currency: :AUD
391+
iex> Money.parse string
392+
#Money<:AUD, 1234.00>
390393
```
391394
# Changelog for Money v3.2.1
392395

lib/money.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,9 +1117,9 @@ defmodule Money do
11171117
{:error,
11181118
{Money.ExchangeRateError, "No exchange rate is available for currency :AUD"}}
11191119
1120-
iex> rates = %{AUD: Decimal.new(2), USD: Decimal.new(1)}
1121-
iex> Money.sum [Money.new(:USD, 100), Money.new(:USD, 200), Money.new(:AUD, 50)], rates
1122-
{:ok, Money.from_float(:USD, 325.0)}
1120+
iex> rates = %{AUD: Decimal.new(2), USD: Decimal.new(1)}
1121+
iex> Money.sum [Money.new(:USD, 100), Money.new(:USD, 200), Money.new(:AUD, 50)], rates
1122+
{:ok, Money.from_float(:USD, 325.0)}
11231123
11241124
"""
11251125
@doc since: "5.3.0"

lib/money/parser/combinators.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ defmodule Money.Combinators do
33

44
import NimbleParsec
55

6-
@whitespace [?\s, ?\t]
6+
# Whitespace as defined by Unicode set :Zs plus tab
7+
@whitespace [?\s, ?\t, 0xa0, 0x1680, 0x2000, 0x202F, 0x205f, 0x3000]
78
def whitespace do
89
repeat(utf8_char(@whitespace))
910
|> label("whitespace")

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"erlsom": {:hex, :erlsom, "1.5.0", "c5a5cdd0ee0e8dca62bcc4b13ff08da24fdefc16ccd8b25282a2fda2ba1be24a", [:rebar3], [], "hexpm", "55a9dbf9cfa77fcfc108bd8e2c4f9f784dea228a8f4b06ea10b684944946955a"},
1919
"ex_cldr": {:hex, :ex_cldr, "2.16.2", "75e344eaeae7c6943cce84220efd45d52e84f907400234f11d2cf0471d00080a", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:cldr_utils, "~> 2.9", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.13", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "04d71af6248d34207eeb4d1b6628848b18e9dbb7c05411d956e930b4a186c746"},
2020
"ex_cldr_currencies": {:hex, :ex_cldr_currencies, "2.6.2", "b7e9e0075d0e90faae24ee2cb1c7eb5b53e547f2d8a5445a78ebc284c4ea5f83", [:mix], [{:ex_cldr, "~> 2.14", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "89dd1913ca3d95c27579564a2abec3fa45b6847a304ffaac6a16f960615cb798"},
21-
"ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.15.2", "5023b2a807117e51faa28c18f75ac976c11a9c75e9278641529079ecdc982684", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.15", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, "~> 2.5", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "6519e22f01d17bdef72dba92ea61f39ce90ee1e57f87a004bb088f99716e391a"},
21+
"ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.15.3", "1fc5f5b3be542664797ea9483d2f1fff7d3c0565ffef0dec4bc53e0b610484fa", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.15", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, "~> 2.5", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "890a0f6286f5ff40d22d70ff35e035729622734f958516ba27a01f804b3ff029"},
2222
"ex_doc": {:hex, :ex_doc, "0.22.2", "03a2a58bdd2ba0d83d004507c4ee113b9c521956938298eba16e55cc4aba4a6c", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "cf60e1b3e2efe317095b6bb79651f83a2c1b3edcb4d319c421d7fcda8b3aff26"},
2323
"exprintf": {:hex, :exprintf, "0.2.1", "b7e895dfb00520cfb7fc1671303b63b37dc3897c59be7cbf1ae62f766a8a0314", [:mix], [], "hexpm", "20a0e8c880be90e56a77fcc82533c5d60c643915c7ce0cc8aa1e06ed6001da28"},
2424
"exprof": {:hex, :exprof, "0.2.3", "8d4d657d73fc0c9ef1e30b2f9207b26ccbd2aec2baf1ca43f0b6d244c841c9f8", [:mix], [{:exprintf, "~> 0.2", [hex: :exprintf, repo: "hexpm", optional: false]}], "hexpm", "040410c672e2403908f578fb1e203a178692bbcd61b11d3745912662532b1dad"},

test/money_parse_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,12 @@ defmodule MoneyTest.Parse do
105105
test "de locale" do
106106
assert Money.parse("1.127,54 €", locale: "de") == Money.new(:EUR, "1127.54")
107107
end
108+
109+
test "Round trip parsing" do
110+
assert Money.parse("1 127,54 €", locale: "fr") ==
111+
Money.new!(:EUR, "1127.54")
112+
|> Money.to_string!(locale: "fr")
113+
|> Money.parse(locale: "fr")
114+
end
108115
end
109116
end

0 commit comments

Comments
 (0)