Skip to content

Commit

Permalink
Merge pull request #200 from geraldodev/decode_int
Browse files Browse the repository at this point in the history
#37 decode int? on cljs different from clj
  • Loading branch information
ikitommi authored May 18, 2020
2 parents 8b2aa9f + fa993bb commit c529192
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/malli/transform.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
(if (string? x)
(try
#?(:clj (Long/parseLong x)
:cljs (let [x' (js/parseInt x 10)]
:cljs (let [x' (if (re-find #"\D" (subs x 1))
##NaN
(js/parseInt x 10))]
(if (js/isNaN x') x x')))
(catch #?(:clj Exception, :cljs js/Error) _ x))
x))
Expand Down
4 changes: 4 additions & 0 deletions test/malli/transform_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
(testing "predicates"
(testing "decode"
(is (= 1 (m/decode int? "1" mt/string-transformer)))
(is (= "1abc" (m/decode int? "1abc" mt/string-transformer)))
(is (= "+1-2" (m/decode int? "+1-2" mt/string-transformer)))
(is (= 1 (m/decode int? "+1" mt/string-transformer)))
(is (= -1 (m/decode int? "-1" mt/string-transformer)))
(is (= "1" (m/decode int? "1" mt/json-transformer)))
(is (= 1.0 (m/decode double? 1 mt/json-transformer)))
(is (= 1 (m/decode double? 1 mt/string-transformer)))
Expand Down

0 comments on commit c529192

Please sign in to comment.