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

Conversation

geraldodev
Copy link

Javascript parseInt is lenient with "+1-2" "1abc" resulting integer one.
Long/parseLong recognizes the sign at the start of the string.
So I've made a \D expression ignoring the first character and return ##Nan if it's found.
Just to be more like clojure. I think we can do this because we are parsing base 10 numbers.
The if string? already guards for string so its safe to call subs and re-find.

@@ -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.

@ikitommi ikitommi merged commit c529192 into metosin:master May 18, 2020
@ikitommi
Copy link
Member

thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants