Skip to content

Commit

Permalink
Added number ns.
Browse files Browse the repository at this point in the history
  • Loading branch information
r0man committed Jul 6, 2012
1 parent 3bb9078 commit 6f81523
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ lib
/.lein-plugins /.lein-plugins
/.crossover-cljs /.crossover-cljs
/stale/dependencies /stale/dependencies
/out
2 changes: 1 addition & 1 deletion index.html
Expand Up @@ -14,7 +14,7 @@
<body> <body>
<div id="content"> <div id="content">
<script type="text/javascript">var CLOSURE_NO_DEPS = true;</script> <script type="text/javascript">var CLOSURE_NO_DEPS = true;</script>
<script type="text/javascript" src="../target/inflections-debug.js"></script> <script type="text/javascript" src="./target/inflections-debug.js"></script>
<script type="text/javascript">inflections.repl.connect();</script> <script type="text/javascript">inflections.repl.connect();</script>
</body> </body>
</html> </html>
14 changes: 10 additions & 4 deletions project.clj
@@ -1,13 +1,19 @@
(defproject inflections "0.7.0-SNAPSHOT" (defproject inflections "0.7.0-SNAPSHOT"
:description "Rails-like inflections for Clojure." :description "Rails-like inflections for Clojure(Script)."
:url "http://github.com/r0man/inflections-clj" :url "http://github.com/r0man/inflections-clj"
:author "Roman Scherer" :author "Roman Scherer"
:min-lein-version "2.0.0" :min-lein-version "2.0.0"
:dependencies [[org.clojure/clojure "1.4.0"] :dependencies [[org.clojure/clojure "1.4.0"]
[org.clojure/clojurescript "0.0-1424"]] [org.clojure/clojurescript "0.0-1443"]]
:plugins [[lein-cljsbuild "0.2.1"]] :plugins [[lein-cljsbuild "0.2.2"]]
:hooks [leiningen.cljsbuild] :hooks [leiningen.cljsbuild]
:cljsbuild {:builds [{:compiler {:output-to "target/inflections-debug.js"} :cljsbuild {:builds [{:compiler {:output-to "target/inflections-test.js"
:optimizations :advanced
:pretty-print true}
:source-path "test/cljs"}
{:compiler {:output-to "target/inflections-debug.js"
:optimizations :whitespace
:pretty-print true}
:source-path "src/cljs"} :source-path "src/cljs"}
{:compiler {:output-to "target/inflections.js" {:compiler {:output-to "target/inflections.js"
:optimizations :advanced :optimizations :advanced
Expand Down
16 changes: 16 additions & 0 deletions src/clj/inflections/number.clj
@@ -0,0 +1,16 @@
(ns inflections.number)

(defn parse-float
"Parse `s` as a float number."
[s] (try (Float/parseFloat (str s))
(catch NumberFormatException _ nil)))

(defn parse-integer
"Parse `s` as a integer."
[s] (try (Integer/parseInt (str s))
(catch NumberFormatException _ nil)))

(defn parse-long
"Parse `s` as a long."
[s] (try (Long/parseLong (str s))
(catch NumberFormatException _ nil)))
11 changes: 11 additions & 0 deletions src/cljs/inflections/number.cljs
@@ -0,0 +1,11 @@
(ns inflections.number)

(defn parse-float
"Parse `s` as a floating-point number."
[s] (let [n (js/parseFloat (str s))]
(if-not (js/isNaN n) n)))

(defn parse-integer
"Parse `s` as a integer."
[s] (let [n (js/parseInt (str s))]
(if-not (js/isNaN n) n)))
5 changes: 5 additions & 0 deletions test-cljs.sh
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
if [ ! -f target/inflections-test.js ]; then
lein cljsbuild once
fi
echo "inflections.test.run()" | d8 --shell target/inflections-test.js
24 changes: 24 additions & 0 deletions test/clj/inflections/test/number.clj
@@ -0,0 +1,24 @@
(ns inflections.test.number
(:use clojure.test
inflections.number))

(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"))))

(deftest test-parse-integer
(is (nil? (parse-integer nil)))
(is (nil? (parse-integer "")))
(is (= 1 (parse-integer "1")))
(is (= 10 (parse-integer "10")))
(is (= -10 (parse-integer "-10"))))

(deftest test-parse-long
(is (nil? (parse-long nil)))
(is (nil? (parse-long "")))
(is (= 1 (parse-long "1")))
(is (= 10 (parse-long "10")))
(is (= -10 (parse-long "-10"))))
8 changes: 8 additions & 0 deletions test/cljs/inflections/test.cljs
@@ -0,0 +1,8 @@
(ns inflections.test
(:require [inflections.test.core :as core]
[inflections.test.number :as number]))

(defn ^:export run []
(core/test)
(number/test)
"All tests passed.")
20 changes: 20 additions & 0 deletions test/cljs/inflections/test/number.cljs
@@ -0,0 +1,20 @@
(ns inflections.test.number
(:require [inflections.number :refer [parse-float parse-integer]]))

(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"))))

(defn test-parse-integer []
(assert (nil? (parse-integer nil)))
(assert (nil? (parse-integer "")))
(assert (= 1 (parse-integer "1")))
(assert (= 10 (parse-integer "10")))
(assert (= -10 (parse-integer "-10"))))

(defn test []
(test-parse-float)
(test-parse-integer))

0 comments on commit 6f81523

Please sign in to comment.