Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#37 decode int? on cljs different from clj #200

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use js/Number here, and test the result with js/Number.isInteger.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would guess the performance is better but also the number logic stays elsewhere. What do you think.

Copy link
Author

@geraldodev geraldodev May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After making the pull. I've tried isFinite to get rid of the re-find. But I've abandoned because it let numbers like '2e2' pass. And it's not on par with clojure's parseLong. Number/isInteger suffers from the same problem. Number('2e2') = 200 . We would be in a better position than today, but not on par with clojure.

##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