Skip to content

Commit

Permalink
Added parse-bytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
r0man committed Feb 4, 2013
1 parent 276b323 commit 02e9212
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 7 deletions.
2 changes: 0 additions & 2 deletions bin/phantomjs-test

This file was deleted.

1 change: 1 addition & 0 deletions bin/v8-test
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ JS_TESTS="target/inflections-test.js"


[ ! -f $JS_TESTS ] && lein cljsbuild once [ ! -f $JS_TESTS ] && lein cljsbuild once



if [ -n "`type d8 > /dev/null 2>&1`" ] ; then if [ -n "`type d8 > /dev/null 2>&1`" ] ; then
echo "inflections.test.run()\n" | d8 --shell $JS_TESTS echo "inflections.test.run()\n" | d8 --shell $JS_TESTS
elif [ -n "`type v8 > /dev/null 2>&1`" ] ; then elif [ -n "`type v8 > /dev/null 2>&1`" ] ; then
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
"resources/repl.html" "resources/repl.html"
:stdout ".repl-phantom-naked-out" :stdout ".repl-phantom-naked-out"
:stderr ".repl-phantom-naked-err"]} :stderr ".repl-phantom-naked-err"]}
:test-commands {;"phantomjs" ["bin/phantomjs-test"] :test-commands {"phantomjs" ["phantomjs" "resources/test.js" "resources/test.html"]
"v8" ["bin/v8-test"]}}) "v8" ["bin/v8-test"]}})
19 changes: 19 additions & 0 deletions src/clj/inflections/util.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
(:refer-clojure :exclude [replace]) (:refer-clojure :exclude [replace])
(:require [clojure.string :refer [upper-case replace split]])) (:require [clojure.string :refer [upper-case replace split]]))


(def byte-scale
{"B" (Math/pow 1024 0)
"K" (Math/pow 1024 1)
"M" (Math/pow 1024 2)
"G" (Math/pow 1024 3)
"T" (Math/pow 1024 4)
"P" (Math/pow 1024 5)
"E" (Math/pow 1024 6)
"Z" (Math/pow 1024 7)
"Y" (Math/pow 1024 8)})

(defn- apply-unit [number unit] (defn- apply-unit [number unit]
(if (string? unit) (if (string? unit)
(case (upper-case unit) (case (upper-case unit)
Expand All @@ -17,6 +28,14 @@
(apply-unit number unit)) (apply-unit number unit))
(catch NumberFormatException _ nil)))) (catch NumberFormatException _ nil))))


(defn parse-bytes [s]
(binding [*read-eval* false]
(if-let [matches (re-matches #"\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)(B|K|M|G|T|P|E|Z|Y)?.*" (str s))]
(let [number (read-string (nth matches 1))
unit (nth matches 3)]
(* (read-string (nth matches 1))
(get byte-scale (upper-case (or unit "")) 1))))))

(defn parse-double (defn parse-double
"Parse `s` as a double number." "Parse `s` as a double number."
[s] (parse-number s #(Double/parseDouble %1))) [s] (parse-number s #(Double/parseDouble %1)))
Expand Down
21 changes: 20 additions & 1 deletion src/cljs/inflections/util.cljs
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,18 @@
(ns inflections.util (ns inflections.util
(:refer-clojure :exclude [replace]) (:refer-clojure :exclude [replace])
(:require [clojure.string :refer [blank? replace split upper-case]])) (:require [clojure.string :refer [blank? replace split upper-case]]
[cljs.reader :refer [read-string]]))

(def byte-scale
{"B" (.pow js/Math 1024 0)
"K" (.pow js/Math 1024 1)
"M" (.pow js/Math 1024 2)
"G" (.pow js/Math 1024 3)
"T" (.pow js/Math 1024 4)
"P" (.pow js/Math 1024 5)
"E" (.pow js/Math 1024 6)
"Z" (.pow js/Math 1024 7)
"Y" (.pow js/Math 1024 8)})


(defn- apply-unit [number unit] (defn- apply-unit [number unit]
(if (string? unit) (if (string? unit)
Expand All @@ -17,6 +29,13 @@
(if-not (js/isNaN number) (if-not (js/isNaN number)
(apply-unit number unit))))) (apply-unit number unit)))))


(defn parse-bytes [s]
(if-let [matches (re-matches #"\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)(B|K|M|G|T|P|E|Z|Y)?.*" (str s))]
(let [number (read-string (nth matches 1))
unit (nth matches 3)]
(* (read-string (nth matches 1))
(get byte-scale (upper-case (or unit "")) 1)))))

(defn parse-float (defn parse-float
"Parse `s` as a float number." "Parse `s` as a float number."
[s] (parse-number s js/parseFloat)) [s] (parse-number s js/parseFloat))
Expand Down
14 changes: 13 additions & 1 deletion test/clj/inflections/test/util.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
(:use clojure.test (:use clojure.test
inflections.util)) inflections.util))


(deftest test-parse-bytes
(is (nil? (parse-bytes nil)))
(is (nil? (parse-bytes "")))
(is (= 1 (parse-bytes "1")))
(is (= 1.0 (parse-bytes "1B")))
(is (= 1.0 (parse-bytes "1.0B")))
(is (= 10.0 (parse-bytes "10.0")))
(is (= -10.0 (parse-bytes "-10.0")))
(is (= 1024.0 (parse-bytes "1K")))
(is (= 1048576.0 (parse-bytes "1M")))
(is (= 1048576.0 (parse-bytes "1.0M"))))

(deftest test-parse-double (deftest test-parse-double
(is (nil? (parse-double nil))) (is (nil? (parse-double nil)))
(is (nil? (parse-double ""))) (is (nil? (parse-double "")))
Expand Down Expand Up @@ -95,4 +107,4 @@
(is (nil? (separator "Message"))) (is (nil? (separator "Message")))
(is (= "." (separator "twitter.hash-tags"))) (is (= "." (separator "twitter.hash-tags")))
(is (= "." (separator "twitter.users"))) (is (= "." (separator "twitter.users")))
(is (= "::" (separator "Admin::Post")))) (is (= "::" (separator "Admin::Post"))))
18 changes: 16 additions & 2 deletions test/cljs/inflections/test/util.cljs
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,18 @@
(ns inflections.test.util (ns inflections.test.util
(:require [inflections.util :refer [parse-double parse-float parse-integer parse-long parse-location parse-url]])) (:require [inflections.util :refer [parse-bytes parse-double parse-float parse-integer]]
[inflections.util :refer [parse-long parse-location parse-url]]))

(defn test-parse-bytes []
(assert (nil? (parse-bytes nil)))
(assert (nil? (parse-bytes "")))
(assert (= 1 (parse-bytes "1")))
(assert (= 1.0 (parse-bytes "1B")))
(assert (= 1.0 (parse-bytes "1.0B")))
(assert (= 10.0 (parse-bytes "10.0")))
(assert (= -10.0 (parse-bytes "-10.0")))
(assert (= 1024.0 (parse-bytes "1K")))
(assert (= 1048576.0 (parse-bytes "1M")))
(assert (= 1048576.0 (parse-bytes "1.0M"))))


(defn test-parse-double [] (defn test-parse-double []
(assert (nil? (parse-double nil))) (assert (nil? (parse-double nil)))
Expand Down Expand Up @@ -78,8 +91,9 @@
(assert (nil? (:query-string spec))))) (assert (nil? (:query-string spec)))))


(defn test [] (defn test []
(test-parse-bytes)
(test-parse-double) (test-parse-double)
(test-parse-float) (test-parse-float)
(test-parse-integer) (test-parse-integer)
(test-parse-location) (test-parse-location)
(test-parse-url)) (test-parse-url))

0 comments on commit 02e9212

Please sign in to comment.