Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #9 from christophermaier/chainable-aget

Chainable aget
  • Loading branch information...
commit 78ed1755ad70abf06788fb40786a14f34d38bb3f 2 parents 3df6a1e + ac6406f
@arohner authored
View
8 README.markdown
@@ -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))
View
6 src/com/reasonr/scriptjure.clj
@@ -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
4 test/test_scriptjure.clj
@@ -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\"]}")))
Please sign in to comment.
Something went wrong with that request. Please try again.