Skip to content

Instantly share code, notes, and snippets.

@viebel
Last active October 19, 2021 08:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viebel/6086821294611d4be3f45ccc28060f2e to your computer and use it in GitHub Desktop.
Save viebel/6086821294611d4be3f45ccc28060f2e to your computer and use it in GitHub Desktop.
Make a ClojureScript object accessible as a JavaScript object without converting it
;; 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