Fork me on GitHub
#clojurescript
<
2022-03-03
>
fadrian12:03:31

I'm attempting to use the camel-snake-kebab library in a clojurescript project. According to the docs, this code should work with both clojure and clojurescript. I added the appropriate :require to my namespace:

(ns atlas.rendering
  (:require 
            [camel-snake-kebab.core :as csk]))
and added the proper dependency to my shadow-cljs.edn file:
:dependencies
 [[camel-snake-kebab "0.4.2"]]
This should work, but I get an error:
The required namespace "camel-snake-kebab.core" is not available, it was required by "atlas/rendering.cljs".
Obviously, there's some reason why cljs isn't finding the library, but I have no idea why. Does anyone have any suggestions?

p-himik13:03:58

Have you restarted the shadow-cljs server?

fadrian13:03:28

That was it. Thanks.

👍 1
fadrian14:03:19

In clojurescript is there any way to convert from json -> edn passing keys to a keyword conversion function? The keys in my json are camel-cased and I'd like to convert them to kebab case, rather than just converting the camel-cased keys to keywords. I've looked at js->clj, but it seems to only take a boolean parameter :keywordize-keys and not a function to convert keys. Is there another library I could use that would allow me to do this? Alternatively, is there an easy way to recursively walk the converted edn converting the map keys after the fact?

p-himik14:03:11

The implementation of js->clj is rather trivial and given that you deal with JSON and not arbitrary JS objects, you can simplify it even further - it just recursively walks the data structure and replaces what it can. Alternatively, you can use clojure.walk. Doesn't matter that much.

p-himik14:03:16

Just in case - note that you needs depth-first traversal, so it would be clojure.walk/postwalk.

fadrian17:03:46

Yes. I used that. It was a bit trickier reading in the JSON and getting it translated into JSON objects before converting it to EDN, but once that was done, getting the keywords correct was relatively simple. Thanks again for your help.

p-himik17:03:14

> It was a bit trickier reading in the JSON and getting it translated into JSON What do you mean? It should be as simple as calling js/JSON.parse on a string.

fadrian16:03:45

I had the json code in a resource file and had to get that into the code. Which sent me onto a macro adventure so I could slurp it into the cljs. The good news is that I now know how to use macros in cljs.

👍 1
borkdude14:03:27

Will Google Closure be able to "cut out" top level maps that are unused? E.g.:

(ns example)
(defn foo [])
(def a {'foo bar}) ;; unused in other namespaces

dnolen14:03:12

@borkdude in the past did not work, but I believe it does work now

borkdude14:03:41

I remember your comment from the past, that's why I asked :)

p-himik14:03:26

Just tested - it does indeed get removed.

borkdude14:03:34

good! thanks!