Permalink
Browse files

Merge pull request #9 from christophermaier/chainable-aget

Chainable aget
  • Loading branch information...
2 parents 3df6a1e + ac6406f commit 78ed1755ad70abf06788fb40786a14f34d38bb3f @arohner committed Jun 10, 2011
Showing with 14 additions and 4 deletions.
  1. +7 −1 README.markdown
  2. +4 −2 src/com/reasonr/scriptjure.clj
  3. +3 −1 test/test_scriptjure.clj
View
@@ -114,15 +114,21 @@ Takes one argument, results in a delete statement
(new Obj & args)
Results in a new statement. The first argument is the object. All remaining items in the list are treated as arguments to the contructor.
+
(js (new google.visualization.Query url))
=> "new google.visualization.Query(url)"
**aget**
- (aget obj index)
+ (aget obj & indexes)
(js (aget foo 42))
=> "foo[42]"
+Array access can also be chained. This is helpful not only for multidimensional arrays, but for reaching deep into objects using a series of keys (similar to `clojure.core/get-in`)
+
+ (js (aget foo bar "baz"))
+ => "foo[bar][\"baz\"]"
+
To set an array, combine with set!
(js (set! (aget foo 42) 13))
@@ -176,8 +176,10 @@
(defmethod emit-special 'new [type [new class & args]]
(str "new " (emit class) (comma-list (map emit args))))
-(defmethod emit-special 'aget [type [aget var idx]]
- (str (emit var) "[" (emit idx) "]"))
+(defmethod emit-special 'aget [type [aget var & idxs]]
+ (apply str
+ (emit var)
+ (interleave (repeat "[") (map emit idxs) (repeat "]"))))
(defmethod emit-special 'inc! [type [inc var]]
(str (emit var) "++"))
View
@@ -86,7 +86,9 @@
(is (= (js [1 "2" :foo]) "[1, \"2\", foo]")))
(deftest test-aget
- (is (= (js (aget foo 2)) "foo[2]")))
+ (is (= (js (aget foo 2)) "foo[2]"))
+ (is (= (js (aget foo bar baz) "foo[bar][baz]")))
+ (is (= (js (aget foo "bar" "baz") "foo[\"bar\"][\"baz\"]"))))
(deftest test-map
(is (= (strip-whitespace (js {:packages ["columnchart"]})) "{packages: [\"columnchart\"]}")))

0 comments on commit 78ed175

Please sign in to comment.