diff --git a/src/monger/js.clj b/src/monger/js.clj new file mode 100644 index 0000000..b443ef5 --- /dev/null +++ b/src/monger/js.clj @@ -0,0 +1,25 @@ +(ns monger.js + (:require [clojure.java.io])) + +;; +;; Implementation +;; + +(defn- normalize-resource + [^String path] + (if (.endsWith path ".js") + path + (str path ".js"))) + + + +;; +;; API +;; + +(defn load-resource + (^String [^String path] + (with-open [rdr (clojure.java.io/reader (-> (Thread/currentThread) + .getContextClassLoader + (.getResourceAsStream (normalize-resource path))))] + (reduce str "" (line-seq rdr))))) \ No newline at end of file diff --git a/test/monger/test/js.clj b/test/monger/test/js.clj new file mode 100644 index 0000000..8308bbf --- /dev/null +++ b/test/monger/test/js.clj @@ -0,0 +1,10 @@ +(ns monger.test.js + (:require [monger js] + [clojure.java.io :only [reader]]) + (:use [clojure.test])) + + +(deftest load-js-resource-using-path-on-the-classpath + (are [c path] (= c (count (monger.js/load-resource path))) + 60 "resources/mongo/js/mapfun1.js" + 60 "resources/mongo/js/mapfun1")) diff --git a/test/resources/mongo/js/mapfun1.js b/test/resources/mongo/js/mapfun1.js new file mode 100644 index 0000000..0c417df --- /dev/null +++ b/test/resources/mongo/js/mapfun1.js @@ -0,0 +1,3 @@ +function() { + emit(this.state, this.price * this.quantity); +} \ No newline at end of file