Skip to content

Commit

Permalink
Rename parsing functions to include string for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
sernamar committed Sep 15, 2024
1 parent ec37080 commit ac2461d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 55 deletions.
28 changes: 14 additions & 14 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -184,52 +184,52 @@ For example:
*** Parsing
This library supports parsing strings with both ISO 4217 currencies (e.g., =:eur=) and non-ISO 4217 currencies (e.g., =:btc=), whether they include currency symbols (e.g., =€= or =₿=) or currency codes (e.g., =EUR= or =BTC=).

To parse a string representing a monetary amount, use the =parse-money= function, which accepts a map of configuration options as its second argument. The available options are:
To parse a string representing a monetary amount, use the =parse-string= function, which accepts a map of configuration options as its second argument. The available options are:
- =:locale=: a =java.util.Locale= object used for parsing. If =NIL=, the default locale is used.
- =:currencies=: a sequence of currencies to attempt during parsing. If =NIL=, it defaults to either the configured currency or the locale's default currency.
- =:try-all-currencies?=: a boolean flag. If =TRUE=, the function will attempt to parse the string using all currencies available in =resources/currencies.edn= if the provided currencies fail. Defaults to =FALSE=.
#+begin_src clojure
(parse/parse-money "1.234,56 €" {:locale java.util.Locale/GERMANY})
(parse/parse-string "1.234,56 €" {:locale java.util.Locale/GERMANY})
;; => {:amount 1234.56M, :currency :eur}

(parse/parse-money "1.234,56 EUR" {:locale java.util.Locale/GERMANY})
(parse/parse-string "1.234,56 EUR" {:locale java.util.Locale/GERMANY})
;; => {:amount 1234.56M, :currency :eur}

(parse/parse-money "1.234,56 £" {:locale java.util.Locale/GERMANY :currencies [:eur :gbp]})
(parse/parse-string "1.234,56 £" {:locale java.util.Locale/GERMANY :currencies [:eur :gbp]})
;; => {:amount 1234.56M, :currency :gbp}

(parse/parse-money "1.234,56 GBP" {:locale java.util.Locale/GERMANY :currencies [:eur :gbp]})
(parse/parse-string "1.234,56 GBP" {:locale java.util.Locale/GERMANY :currencies [:eur :gbp]})
;; => {:amount 1234.56M, :currency :gbp}

(parse/parse-money "1.234,56 £" {:locale java.util.Locale/GERMANY :try-all-currencies? true})
(parse/parse-string "1.234,56 £" {:locale java.util.Locale/GERMANY :try-all-currencies? true})
;; => {:amount 1234.56M, :currency :gbp}

(parse/parse-money "1.234,56 ₿" {:locale java.util.Locale/GERMANY :currencies [:btc]})
(parse/parse-string "1.234,56 ₿" {:locale java.util.Locale/GERMANY :currencies [:btc]})
;; => {:amount 1234.56M, :currency :btc}

(parse/parse-money "1.234,56 BTC" {:locale java.util.Locale/GERMANY :currencies [:btc]})
(parse/parse-string "1.234,56 BTC" {:locale java.util.Locale/GERMANY :currencies [:btc]})
;; => {:amount 1234.56M, :currency :btc}

(parse/parse-money "1.234,56 ₿" {:locale java.util.Locale/GERMANY :try-all-currencies? true})
(parse/parse-string "1.234,56 ₿" {:locale java.util.Locale/GERMANY :try-all-currencies? true})
;; => {:amount 1234.56M, :currency :btc}

(parse/parse-money "1.234,56 ₿" {:locale java.util.Locale/GERMANY :currencies [:eur :gbp] :try-all-currencies? true})
(parse/parse-string "1.234,56 ₿" {:locale java.util.Locale/GERMANY :currencies [:eur :gbp] :try-all-currencies? true})
;; => {:amount 1234.56M, :currency :btc}
#+end_src
If =parse-money= cannot recognize the format or the currency in the string, it throws a =java.text.ParseException=:
If =parse-string= cannot recognize the format or the currency in the string, it throws a =java.text.ParseException=:
#+begin_src clojure
;; unrecognized format for java.util.Locale/GERMANY
(parse/parse-money "1,234.56 €" {:locale java.util.Locale/GERMANY})
(parse/parse-string "1,234.56 €" {:locale java.util.Locale/GERMANY})
;; Unhandled java.text.ParseException
;; Unparseable number: "1,234.56 €"

;; unrecognized currency for java.util.Locale/GERMANY
(parse/parse-money "1.234,56 £" {:locale java.util.Locale/GERMANY})
(parse/parse-string "1.234,56 £" {:locale java.util.Locale/GERMANY})
;; Unhandled java.text.ParseException
;; Unparseable number: "1.234,56 £"

;; unrecognized currency for any java.util.Locale
(parse/parse-money "1.234,56 ₿" {:locale java.util.Locale/GERMANY})
(parse/parse-string "1.234,56 ₿" {:locale java.util.Locale/GERMANY})
;; Unhandled java.text.ParseException
;; Unparseable number: "1.234,56 ₿"
#+end_src
Expand Down
16 changes: 8 additions & 8 deletions src/dinero/parse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
(DecimalFormat/.setDecimalFormatSymbols formatter symbols)
formatter))

(defn parse-with-symbol-or-code
(defn parse-string-with-symbol-or-code
"Parses a monetary string using currency symbol or code."
[string locale currency]
(try
Expand All @@ -40,21 +40,21 @@
amount (DecimalFormat/.parse ^DecimalFormat formatter string)]
(core/money-of amount currency)))))

(defn attempt-parse-with-multiple-currencies
(defn attempt-parse-string-with-multiple-currencies
"Tries to parse a monetary string using a list of currencies."
[string locale currencies]
(or (some #(try (parse-with-symbol-or-code string locale %)
(or (some #(try (parse-string-with-symbol-or-code string locale %)
(catch ParseException _e
nil)) ;; ignore parse exceptions
currencies)
(throw (ParseException. (str "Unparseable number: \"" string "\"") 0))))

(defn attempt-parse-with-all-currencies
(defn attempt-parse-string-with-all-currencies
"Tries to parse a monetary string using all available currencies in the `resources/currencies.edn` file."
[string locale]
(attempt-parse-with-multiple-currencies string locale (currency/get-all-currencies)))
(attempt-parse-string-with-multiple-currencies string locale (currency/get-all-currencies)))

(defn parse-money
(defn parse-string
"Parses a monetary string with optional locale and currency settings."
[string & {:keys [locale currencies try-all-currencies?] :as _options}]
(let [locale (or locale (Locale/getDefault))
Expand All @@ -64,8 +64,8 @@
currencies (or currencies [core/*default-currency*] [locale-currency])
try-all-currencies? (or try-all-currencies? false)]
(try
(attempt-parse-with-multiple-currencies string locale currencies)
(attempt-parse-string-with-multiple-currencies string locale currencies)
(catch ParseException e
(if try-all-currencies?
(attempt-parse-with-all-currencies string locale)
(attempt-parse-string-with-all-currencies string locale)
(throw e))))))
66 changes: 33 additions & 33 deletions test/dinero/parse_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,53 @@
(:import [java.text ParseException]
[java.util Locale]))

(t/deftest parse-with-symbol-or-code
(t/deftest parse-string-with-symbol-or-code
(let [m1 (core/money-of 1234.56 :eur)
m2 (core/money-of 1234.56 :gbp)
germany Locale/GERMANY
uk Locale/UK]
(t/is (= m1 (sut/parse-with-symbol-or-code "1.234,56 €" germany :eur)))
(t/is (= m1 (sut/parse-with-symbol-or-code "1.234,56 EUR" germany :eur)))
(t/is (= m2 (sut/parse-with-symbol-or-code "1.234,56 £" germany :gbp)))
(t/is (= m2 (sut/parse-with-symbol-or-code "1.234,56 GBP" germany :gbp)))
(t/is (= m2 (sut/parse-with-symbol-or-code "£1,234.56" uk :gbp)))
(t/is (= m2 (sut/parse-with-symbol-or-code "GBP1,234.56" uk :gbp)))
(t/is (thrown? ParseException (sut/parse-with-symbol-or-code "1.234,56 €" germany :gbp)))
(t/is (thrown? ParseException (sut/parse-with-symbol-or-code "£1,234.56" germany :gbp)))))
(t/is (= m1 (sut/parse-string-with-symbol-or-code "1.234,56 €" germany :eur)))
(t/is (= m1 (sut/parse-string-with-symbol-or-code "1.234,56 EUR" germany :eur)))
(t/is (= m2 (sut/parse-string-with-symbol-or-code "1.234,56 £" germany :gbp)))
(t/is (= m2 (sut/parse-string-with-symbol-or-code "1.234,56 GBP" germany :gbp)))
(t/is (= m2 (sut/parse-string-with-symbol-or-code "£1,234.56" uk :gbp)))
(t/is (= m2 (sut/parse-string-with-symbol-or-code "GBP1,234.56" uk :gbp)))
(t/is (thrown? ParseException (sut/parse-string-with-symbol-or-code "1.234,56 €" germany :gbp)))
(t/is (thrown? ParseException (sut/parse-string-with-symbol-or-code "£1,234.56" germany :gbp)))))

(t/deftest attempt-parse-with-multiple-currencies
(t/deftest attempt-parse-string-with-multiple-currencies
(let [m1 (core/money-of 1234.56 :eur)
germany Locale/GERMANY]
(t/is (= m1 (sut/attempt-parse-with-multiple-currencies "1.234,56 €" germany [:eur :gbp])))
(t/is (= m1 (sut/attempt-parse-with-multiple-currencies "1.234,56 €" germany [:gbp :eur])))
(t/is (thrown? ParseException (sut/attempt-parse-with-multiple-currencies "1.234,56 €" germany [:gbp :btc])))
(t/is (thrown? ParseException (sut/attempt-parse-with-multiple-currencies "1.234,56 ₿" germany [:eur :gbp])))))
(t/is (= m1 (sut/attempt-parse-string-with-multiple-currencies "1.234,56 €" germany [:eur :gbp])))
(t/is (= m1 (sut/attempt-parse-string-with-multiple-currencies "1.234,56 €" germany [:gbp :eur])))
(t/is (thrown? ParseException (sut/attempt-parse-string-with-multiple-currencies "1.234,56 €" germany [:gbp :btc])))
(t/is (thrown? ParseException (sut/attempt-parse-string-with-multiple-currencies "1.234,56 ₿" germany [:eur :gbp])))))

(t/deftest attempt-parse-with-all-currencies
(t/deftest attempt-parse-string-with-all-currencies
(let [m1 (core/money-of 1234.56 :eur)
m2 (core/money-of 1234.56 :gbp)
m3 (core/money-of 1234.56 :btc)
germany Locale/GERMANY]
(t/is (= m1 (sut/attempt-parse-with-all-currencies "1.234,56 €" germany)))
(t/is (= m1 (sut/attempt-parse-with-all-currencies "1.234,56 EUR" germany)))
(t/is (= m2 (sut/attempt-parse-with-all-currencies "1.234,56 £" germany)))
(t/is (= m2 (sut/attempt-parse-with-all-currencies "1.234,56 GBP" germany)))
(t/is (= m3 (sut/attempt-parse-with-all-currencies "1.234,56 ₿" germany)))
(t/is (= m3 (sut/attempt-parse-with-all-currencies "1.234,56 BTC" germany)))
(t/is (thrown? ParseException (sut/attempt-parse-with-all-currencies "1,234.56 €" germany)))))
(t/is (= m1 (sut/attempt-parse-string-with-all-currencies "1.234,56 €" germany)))
(t/is (= m1 (sut/attempt-parse-string-with-all-currencies "1.234,56 EUR" germany)))
(t/is (= m2 (sut/attempt-parse-string-with-all-currencies "1.234,56 £" germany)))
(t/is (= m2 (sut/attempt-parse-string-with-all-currencies "1.234,56 GBP" germany)))
(t/is (= m3 (sut/attempt-parse-string-with-all-currencies "1.234,56 ₿" germany)))
(t/is (= m3 (sut/attempt-parse-string-with-all-currencies "1.234,56 BTC" germany)))
(t/is (thrown? ParseException (sut/attempt-parse-string-with-all-currencies "1,234.56 €" germany)))))

(t/deftest parse-money
(t/deftest parse-string
(let [m1 (core/money-of 1234.56 :eur)
m2 (core/money-of 1234.56 :gbp)
m3 (core/money-of 1234.56 :btc)
germany Locale/GERMANY]
(t/is (= m1 (sut/parse-money "1.234,56 €" {:locale germany})))
(t/is (= m1 (sut/parse-money "1.234,56 €" {:locale germany :currencies [:eur :gbp]})))
(t/is (= m1 (sut/parse-money "1.234,56 €" {:locale germany :currencies [:gbp :eur]})))
(t/is (thrown? ParseException (sut/parse-money "1.234,56 £" {:locale germany})))
(t/is (= m2 (sut/parse-money "1.234,56 £" {:locale germany :try-all-currencies? true})))
(t/is (= m2 (sut/parse-money "1.234,56 £" {:locale germany :currencies [:eur :gbp]})))
(t/is (thrown? ParseException (sut/parse-money "1.234,56 ₿" {:locale germany})))
(t/is (= m3 (sut/parse-money "1.234,56 ₿" {:locale germany :try-all-currencies? true})))
(t/is (= m3 (sut/parse-money "1.234,56 ₿" {:locale germany :currencies [:eur :gbp :btc]})))
(t/is (thrown? ParseException (sut/parse-money "1,234.56 €" {:locale germany})))))
(t/is (= m1 (sut/parse-string "1.234,56 €" {:locale germany})))
(t/is (= m1 (sut/parse-string "1.234,56 €" {:locale germany :currencies [:eur :gbp]})))
(t/is (= m1 (sut/parse-string "1.234,56 €" {:locale germany :currencies [:gbp :eur]})))
(t/is (thrown? ParseException (sut/parse-string "1.234,56 £" {:locale germany})))
(t/is (= m2 (sut/parse-string "1.234,56 £" {:locale germany :try-all-currencies? true})))
(t/is (= m2 (sut/parse-string "1.234,56 £" {:locale germany :currencies [:eur :gbp]})))
(t/is (thrown? ParseException (sut/parse-string "1.234,56 ₿" {:locale germany})))
(t/is (= m3 (sut/parse-string "1.234,56 ₿" {:locale germany :try-all-currencies? true})))
(t/is (= m3 (sut/parse-string "1.234,56 ₿" {:locale germany :currencies [:eur :gbp :btc]})))
(t/is (thrown? ParseException (sut/parse-string "1,234.56 €" {:locale germany})))))

0 comments on commit ac2461d

Please sign in to comment.