Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle units when parsing numbers.

  • Loading branch information...
commit 75b6c63b188b4648b7f2689944075cd65b9712d3 1 parent 9da47d4
@r0man authored
View
0  .repl-phantom-naked-err
No changes.
View
5 .repl-phantom-naked-out
@@ -1,5 +0,0 @@
-Loading URL: resources/repl.html
-TypeError: 'undefined' is not an object (evaluating 'inflections.repl.connect')
-
- file:///home/roman/workspace/inflections-clj/resources/repl.html:7
-Loaded successfully.
View
2  resources/repl.html
@@ -2,7 +2,7 @@
<body>
This is just a dummy HTML file to connect to the REPL.
Don't close this window until you're done with the REPL.
- <script src="../target/inflections.js" type="text/javascript"></script>
+ <script src="../target/inflections-test.js" type="text/javascript"></script>
<script type="text/javascript">
inflections.repl.connect();
</script>
View
22 src/clj/inflections/util.clj
@@ -2,15 +2,25 @@
(:refer-clojure :exclude [replace])
(:require [clojure.string :refer [replace split]]))
-(defn parse-double
- "Parse `s` as a double number."
- [s] (try (Double/parseDouble (str s))
- (catch NumberFormatException _ nil)))
+(defn parse-double [s]
+ (if-let [matches (re-matches #"\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)(M|B)?\s*" (str s))]
+ (let [number (Double/parseDouble (nth matches 1))]
+ (if-let [unit (nth matches 3)]
+ (case unit
+ "M" (* number (Math/pow 10 6))
+ "B" (* number (Math/pow 10 9)))
+ number))))
(defn parse-float
"Parse `s` as a float number."
- [s] (try (Float/parseFloat (str s))
- (catch NumberFormatException _ nil)))
+ [s]
+ (if-let [matches (re-matches #"\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)(M|B)?\s*" (str s))]
+ (let [number (Float/parseFloat (nth matches 1))]
+ (if-let [unit (nth matches 3)]
+ (case unit
+ "M" (* number (Math/pow 10 6))
+ "B" (* number (Math/pow 10 9)))
+ number))))
(defn parse-integer
"Parse `s` as a integer."
View
25 src/cljs/inflections/util.cljs
@@ -1,16 +1,19 @@
(ns inflections.util
(:refer-clojure :exclude [replace])
- (:require [clojure.string :refer [replace split]]))
+ (:require [clojure.string :refer [blank? replace split]]))
-(defn parse-double
- "Parse `s` as a double number."
- [s] (let [n (js/parseFloat (str s))]
- (if-not (js/isNaN n) n)))
+(defn parse-float [s]
+ (if-let [matches (re-matches #"\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)(M|B)?\s*" (str s))]
+ (let [number (js/parseFloat (nth matches 1))]
+ (if-let [unit (nth matches 3)]
+ (case unit
+ "M" (* number (.pow js/Math 10 6))
+ "B" (* number (.pow js/Math 10 9)))
+ number))))
-(defn parse-float
+(defn parse-double
"Parse `s` as a floating-point number."
- [s] (let [n (js/parseFloat (str s))]
- (if-not (js/isNaN n) n)))
+ [s] (parse-float s))
(defn parse-integer
"Parse `s` as a integer."
@@ -23,9 +26,9 @@
(let [regex #"(,)|(\s+)"
[latitude longitude]
(->> (split (str s) regex)
- (remove #(or (blank? %1)
- (re-matches regex (str %1))))
- (map parse-double))]
+ (remove #(or (blank? %1)
+ (re-matches regex (str %1))))
+ (map parse-double))]
(when (and latitude longitude)
{:latitude latitude
:longitude longitude})))
View
12 test/clj/inflections/test/util.clj
@@ -7,14 +7,22 @@
(is (nil? (parse-double "")))
(is (= 1.0 (parse-double "1")))
(is (= 10.0 (parse-double "10.0")))
- (is (= -10.0 (parse-double "-10.0"))))
+ (is (= -10.0 (parse-double "-10.0")))
+ (is (= 1000000.0 (parse-double "1M")))
+ (is (= 1000000.0 (parse-double "1.0M")))
+ (is (= 1000000000.0 (parse-double "1B")))
+ (is (= 1000000000.0 (parse-double "1.0B"))))
(deftest test-parse-float
(is (nil? (parse-float nil)))
(is (nil? (parse-float "")))
(is (= 1.0 (parse-float "1")))
(is (= 10.0 (parse-float "10.0")))
- (is (= -10.0 (parse-float "-10.0"))))
+ (is (= -10.0 (parse-float "-10.0")))
+ (is (= 1000000.0 (parse-float "1M")))
+ (is (= 1000000.0 (parse-float "1.0M")))
+ (is (= 1000000000.0 (parse-float "1B")))
+ (is (= 1000000000.0 (parse-float "1.0B"))) )
(deftest test-parse-integer
(is (nil? (parse-integer nil)))
View
12 test/cljs/inflections/test/util.cljs
@@ -6,14 +6,22 @@
(assert (nil? (parse-double "")))
(assert (= 1.0 (parse-double "1")))
(assert (= 10.0 (parse-double "10.0")))
- (assert (= -10.0 (parse-double "-10.0"))))
+ (assert (= -10.0 (parse-double "-10.0")))
+ (assert (= 1000000.0 (parse-double "1M")))
+ (assert (= 1000000.0 (parse-double "1.0M")))
+ (assert (= 1000000000.0 (parse-double "1B")))
+ (assert (= 1000000000.0 (parse-double "1.0B"))))
(defn test-parse-float []
(assert (nil? (parse-float nil)))
(assert (nil? (parse-float "")))
(assert (= 1.0 (parse-float "1")))
(assert (= 10.0 (parse-float "10.0")))
- (assert (= -10.0 (parse-float "-10.0"))))
+ (assert (= -10.0 (parse-float "-10.0")))
+ (assert (= 1000000.0 (parse-double "1M")))
+ (assert (= 1000000.0 (parse-double "1.0M")))
+ (assert (= 1000000000.0 (parse-double "1B")))
+ (assert (= 1000000000.0 (parse-double "1.0B"))))
(defn test-parse-integer []
(assert (nil? (parse-integer nil)))
Please sign in to comment.
Something went wrong with that request. Please try again.