Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added parse-bytes.

  • Loading branch information...
commit 02e9212fe114b07982f677cd15490d25a0b02191 1 parent 276b323
@r0man authored
View
2  bin/phantomjs-test
@@ -1,2 +0,0 @@
-#!/usr/bin/env bash
-phantomjs resources/test.js resources/test.html
View
1  bin/v8-test
@@ -4,6 +4,7 @@ JS_TESTS="target/inflections-test.js"
[ ! -f $JS_TESTS ] && lein cljsbuild once
+
if [ -n "`type d8 > /dev/null 2>&1`" ] ; then
echo "inflections.test.run()\n" | d8 --shell $JS_TESTS
elif [ -n "`type v8 > /dev/null 2>&1`" ] ; then
View
2  project.clj
@@ -32,5 +32,5 @@
"resources/repl.html"
:stdout ".repl-phantom-naked-out"
: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"]}})
View
19 src/clj/inflections/util.clj
@@ -2,6 +2,17 @@
(:refer-clojure :exclude [replace])
(: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]
(if (string? unit)
(case (upper-case unit)
@@ -17,6 +28,14 @@
(apply-unit number unit))
(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
"Parse `s` as a double number."
[s] (parse-number s #(Double/parseDouble %1)))
View
21 src/cljs/inflections/util.cljs
@@ -1,6 +1,18 @@
(ns inflections.util
(: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]
(if (string? unit)
@@ -17,6 +29,13 @@
(if-not (js/isNaN number)
(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
"Parse `s` as a float number."
[s] (parse-number s js/parseFloat))
View
14 test/clj/inflections/test/util.clj
@@ -2,6 +2,18 @@
(:use clojure.test
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
(is (nil? (parse-double nil)))
(is (nil? (parse-double "")))
@@ -95,4 +107,4 @@
(is (nil? (separator "Message")))
(is (= "." (separator "twitter.hash-tags")))
(is (= "." (separator "twitter.users")))
- (is (= "::" (separator "Admin::Post"))))
+ (is (= "::" (separator "Admin::Post"))))
View
18 test/cljs/inflections/test/util.cljs
@@ -1,5 +1,18 @@
(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 []
(assert (nil? (parse-double nil)))
@@ -78,8 +91,9 @@
(assert (nil? (:query-string spec)))))
(defn test []
+ (test-parse-bytes)
(test-parse-double)
(test-parse-float)
(test-parse-integer)
(test-parse-location)
- (test-parse-url))
+ (test-parse-url))
Please sign in to comment.
Something went wrong with that request. Please try again.