Fork me on GitHub
#clojurescript
<
2023-03-31
>
Jakub Šťastný02:03:54

Is there not a non-macro way of getting a JS property?

(.. theme -colorSchemes -light -palette -bm -test -x))
This is fine, except that -light is hardcoded for now. It's actually a variable mode. So what I really want is this:
(.. theme -colorSchemes mode -palette -bm -test -x))
Except that this won't work, since it's a macro form and it won't expand. It feels rather silly having to wrap such a simple thing in a helper macro just so I can have my var eval'd. Is there a better way? Esp. since macros in CLJS are a bit of a pain.

hifumi12302:03:14

Have you tried using goog.object/getValueByKeys ?

(def example #js {:nested #js {:one 1 :two 2}})

(defn get-nested [num]
  (goog.object/getValueByKeys example #js ["nested" num]))

(get-nested "one") ;=> 1
(get-nested "two") ;=> 2

👍 2
Jakub Šťastný02:03:05

And me writing this ugly macro in the meantime

(defmacro get-colour [theme mode & parts]
    `(do ;(prn :get-colour :mode ~mode)
         (if (= ~mode :light)
           (.. ~theme -colorSchemes -light -palette ~@parts)
           (.. ~theme -colorSchemes -dark -palette ~@parts))))
🙈

hifumi12303:03:24

That macro also seems to work, it’s just not as flexible as a function 🙂

hifumi12303:03:06

This thread has some useful functions from closure library that are hard to find, but they’re with you at all times if you use CLJS. https://clojureverse.org/t/cljs-hidden-google-closure-library-gems/2321

clojure-spin 2
❤️ 2
thheller05:03:33

(defn get-nested [mode]
  (-> theme .-colorSchemes (gobj/get mode) .-pallette))
  

👍 2
thheller05:03:20

if there is only one dynamic part you can still leave the rest hard coded, just using -> instead of .. is more flexible