Permalink
Browse files

Added number ns.

  • Loading branch information...
1 parent 3bb9078 commit 6f815233bcd37f2834a96c8ad62ebc7c05baa545 @r0man committed Jul 6, 2012
View
@@ -11,3 +11,4 @@ lib
/.lein-plugins
/.crossover-cljs
/stale/dependencies
+/out
View
@@ -14,7 +14,7 @@
<body>
<div id="content">
<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>
</body>
</html>
View
@@ -1,13 +1,19 @@
(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"
:author "Roman Scherer"
:min-lein-version "2.0.0"
:dependencies [[org.clojure/clojure "1.4.0"]
- [org.clojure/clojurescript "0.0-1424"]]
- :plugins [[lein-cljsbuild "0.2.1"]]
+ [org.clojure/clojurescript "0.0-1443"]]
+ :plugins [[lein-cljsbuild "0.2.2"]]
: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"}
{:compiler {:output-to "target/inflections.js"
:optimizations :advanced
@@ -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)))
@@ -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)))
View
@@ -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
@@ -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"))))
@@ -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.")
@@ -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.