Skip to content
Snippets Groups Projects
Unverified Commit 1546661b authored by lbrdnk's avatar lbrdnk Committed by GitHub
Browse files

Move `parse-currency` to `util.jvm` ns (#45939)

* Move parse-currency to jvm-util ns

* Use util.jvm ns
parent cb66e220
Branches
Tags
No related merge requests found
......@@ -48,6 +48,7 @@
full-exception-chain
generate-nano-id
host-port-up?
parse-currency
poll
host-up?
ip-address?
......@@ -723,30 +724,6 @@
[hours]
(-> (* 60 hours) minutes->seconds seconds->ms))
(defn parse-currency
"Parse a currency String to a BigDecimal. Handles a variety of different formats, such as:
$1,000.00
-£127.54
-127,54 €
kr-127,54
€ 127,54-
¥200"
^java.math.BigDecimal [^String s]
(when-not (str/blank? s)
(#?(:clj bigdec :cljs js/parseFloat)
(reduce
(partial apply str/replace)
s
[;; strip out any current symbols
[#"[^\d,.-]+" ""]
;; now strip out any thousands separators
[#"(?<=\d)[,.](\d{3})" "$1"]
;; now replace a comma decimal seperator with a period
[#"," "."]
;; move minus sign at end to front
[#"(^[^-]+)-$" "-$1"]]))))
(defn email->domain
"Extract the domain portion of an `email-address`.
......
......@@ -317,3 +317,28 @@
(do
(Thread/sleep (long interval-ms))
(recur)))))))))
;; Following function is not compatible with Safari 16.3 and older because it uses lookbehind regex.
(defn parse-currency
"Parse a currency String to a BigDecimal. Handles a variety of different formats, such as:
$1,000.00
-£127.54
-127,54 €
kr-127,54
€ 127,54-
¥200"
^java.math.BigDecimal [^String s]
(when-not (str/blank? s)
(bigdec
(reduce
(partial apply str/replace)
s
[;; strip out any current symbols
[#"[^\d,.-]+" ""]
;; now strip out any thousands separators
[#"(?<=\d)[,.](\d{3})" "$1"]
;; now replace a comma decimal seperator with a period
[#"," "."]
;; move minus sign at end to front
[#"(^[^-]+)-$" "-$1"]]))))
......@@ -83,3 +83,24 @@
(map ex-message (u/full-exception-chain e))))
(is (= [{:a 1} {:b 2} {:c 3}]
(map ex-data (u/full-exception-chain e)))))))
(deftest ^:parallel parse-currency-test
(are [s expected] (= expected
(u/parse-currency s))
nil nil
"" nil
" " nil
"$1,000" 1000.0M
"$1,000,000" 1000000.0M
"$1,000.00" 1000.0M
"€1.000" 1000.0M
"€1.000,00" 1000.0M
"€1.000.000,00" 1000000.0M
"-£127.54" -127.54M
"-127,54 €" -127.54M
"kr-127,54" -127.54M
"€ 127,54-" -127.54M
"¥200" 200.0M
"¥200." 200.0M
"$.05" 0.05M
"0.05" 0.05M))
......@@ -279,27 +279,6 @@
(u/capitalize-en "IBIS")
(u/capitalize-en "Ibis"))))))
(deftest ^:parallel parse-currency-test
(are [s expected] (= expected
(u/parse-currency s))
nil nil
"" nil
" " nil
"$1,000" 1000.0M
"$1,000,000" 1000000.0M
"$1,000.00" 1000.0M
"€1.000" 1000.0M
"€1.000,00" 1000.0M
"€1.000.000,00" 1000000.0M
"-£127.54" -127.54M
"-127,54 €" -127.54M
"kr-127,54" -127.54M
"€ 127,54-" -127.54M
"¥200" 200.0M
"¥200." 200.0M
"$.05" 0.05M
"0.05" 0.05M))
(deftest ^:parallel email->domain-test
(are [domain email] (= domain
(u/email->domain email))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment