Last active
October 19, 2021 08:54
-
-
Save viebel/6086821294611d4be3f45ccc28060f2e to your computer and use it in GitHub Desktop.
Make a ClojureScript object accessible as a JavaScript object without converting it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy | |
(defn proxy [m] | |
(js/Proxy. m #js {:get (fn [target prop] | |
(let [prop' (if (vector? target) | |
(js/parseInt prop) | |
(keyword prop))] | |
(let [v (or (get target prop') | |
(get target prop))] | |
(if (coll? v) | |
(proxy v) | |
v))))})) | |
(comment | |
(def deeply-nested {:a {:b [{:c {:d 1}}]}}) | |
(-> (proxy deeply-nested) | |
(aget "a") | |
(aget "b") | |
(aget 0) | |
(aget "c") | |
(aget "d")) ;; => 1 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment