funcool / cuerdas

String manipulation library for Clojure(Script)
https://cljdoc.org/d/funcool/cuerdas/2020.03.26-3/
BSD 2-Clause "Simplified" License
304 stars 30 forks source link

Inconsistent parse-number & parse-double behaviours on zero-padded strings #78

Closed rlhk closed 4 years ago

rlhk commented 4 years ago

We should fix parse-number behaviour. Happy to submit fix if agreed.

Clojure:

user=> (str/parse-double "00898")
898.0
user=> (str/parse-int "00898")
898
user=> (str/parse-number "00898")
Execution error (NumberFormatException) at cuerdas.core/parse-number (core.cljc:646).
Invalid number: 00898

ClojureScript:

cljs.user=> (str/parse-double "00898")
898
cljs.user=> (str/parse-int "00898")
898
cljs.user=> (str/parse-number "00898")
Error: Invalid number: 00898.
    at new cljs$core$ExceptionInfo (../js/cljs-runtime/cljs.core.js:36225:10)
    at Function.eval [as cljs$core$IFn$_invoke$arity$3] (../js/cljs-runtime/cljs.core.js:36286:9)
    at Function.eval [as cljs$core$IFn$_invoke$arity$2] (../js/cljs-runtime/cljs.core.js:36282:26)
    at Function.eval [as cljs$core$IFn$_invoke$arity$variadic] (../js/cljs-runtime/cljs.tools.reader.impl.errors.js:52:25)
    at Function.eval [as cljs$core$IFn$_invoke$arity$variadic] (../js/cljs-runtime/cljs.tools.reader.impl.errors.js:92:47)
    at Object.cljs$tools$reader$impl$errors$throw_invalid_number [as throw_invalid_number] (../js/cljs-runtime/cljs.tools.reader.impl.errors.js:212:51)
    at Object.cljs$tools$reader$edn$read_number [as read_number] (../js/cljs-runtime/cljs.tools.reader.edn.js:401:38)
    at Function.eval [as cljs$core$IFn$_invoke$arity$4] (../js/cljs-runtime/cljs.tools.reader.edn.js:852:30)
    at Function.eval [as cljs$core$IFn$_invoke$arity$2] (../js/cljs-runtime/cljs.tools.reader.edn.js:835:35)
    at Function.eval [as cljs$core$IFn$_invoke$arity$2] (../js/cljs-runtime/cljs.tools.reader.edn.js:929:35)
niwinz commented 4 years ago

where are the inconsistencies? seems like they are consistent

rlhk commented 4 years ago

where are the inconsistencies? seems like they are consistent

For cuerdas' tolerance nature, think (str/parse-number "00898") should return a double 898 instead of throwing?

Like

(= (str/parse-number "00898") (str/parse-double))
niwinz commented 4 years ago

You are right, on the end i think i will remove this functions because they are also inconsistent in return type and in fact cuerdas is a string manipulation library not a numbers parsing library; so i think this functions should live outside of cuerdas.

rlhk commented 4 years ago

Agreed. Thanks for replying.