Fork me on GitHub
#clojurescript
<
2021-04-21
>
Mario C.15:04:34

I am creating a simple UI to display a graph and I am using Cytoscape. But I am having a really hard time working with a mutable structure and using JS library functions. > "Should i be passing a cljs map into this function or should I be casting it to JS?" As a workaround I am using Cytoscape just to render the graph and offloading all the graph functionality to the server. Just to make the client side a little more "functional"/immutable. Is this a valid approach? Or am I not understanding something basic with using JS libraries in CLJS that could make this a lot easier?

bwstearns15:04:00

it’s been a minute since I’ve written a lot of clojure but I’m looking at using instaparse as part of a project that’ll run in a JS context. I’d still like the project to be portable between clj and cljs. Is cljx still a thing? I am looking at stuff like json parsing libraries and they all seem to be either JS or JVM based and not cross compatible.

nilern15:04:33

cljx has been replaced by reader conditionals in core

bwstearns15:04:25

ok so all the files are clj now and you just have to reader conditional the JVM/JS specific stuff out if you want to keep it cross platform?

nilern15:04:59

.cljc is the new .cljx

nilern16:04:25

I find many libs are cross platform now. Maybe not ones that are mainly wrappers.

nilern16:04:11

Instaparse seems to support cljs as well

bwstearns16:04:47

cool. Do you have any good recommendation on a cross env way of making json strings into clojure data structures? data.json looks like clj only?

bwstearns16:04:22

Seems like it really should be a non-env specific library.

dpsutton16:04:23

in cljs you can use JSON/parse and then you just handle js datastructures to cljs datastructures as you like. js->clj, cljs-bean, whatever

bwstearns16:04:32

ok cool. so there’s no canonical json parsing lib that works on both JS and JVM?

dpsutton16:04:33

clj -A:cljs -M -m cljs.main -re node -r
ClojureScript 1.10.773
cljs.user=> ((comp js->clj #(.parse js/JSON %)) "{\"a\": 1}")
{"a" 1}
cljs.user=> (type *1)
cljs.core/PersistentArrayMap
cljs.user=>

nilern16:04:51

A cross-platform JSON library based on say, Jackson and JSON.parse would share almost no code between JVM and JS so I guess no one has bothered And you can roll your own in like one function

dpsutton16:04:53

not that i'm aware of. because on js json parsing is native

bwstearns16:04:24

ah that makes sense. I hadn’t thought about that aspect.

bwstearns16:04:57

Thanks for catching me up. I’ll just reader conditional the parsing steps.

👍 3
nilern16:04:48

(defn read-json-str [s] #?(:clj (jsonista/read-value s), :cljs (.parse js/JSON s)))
Or whatever

👍 3
dpsutton16:04:10

beware that one branch will return clojure datastructures and one branch will return js datastructures. that's why i was using js->clj above. An equivalent would be using jackson and getting back java maps and arrays rather than immutable hashmaps and vectors

nilern16:04:40

Oops sorry about that

nilern16:04:01

(defn read-json-str [s] #?(:clj (jsonista/read-value s), :cljs (js->clj (.parse js/JSON s))))

nilern16:04:26

And all the JVM JSON libs and js->clj support keywordizing keys but a bit differently

p-himik16:04:50

Just in case - note that js->clj has some unlikely but still probable issues. It uses satisfies? and implements? and IIRC it's possible to create data that would trigger those branches.

p-himik16:04:18

Oh, and this: (identical? (type x) js/Object). So if your JSON data has fields named constructor, js->clj will produce incorrect results.

Emi Gal18:04:28

Hi clojurians! Is there a good clojurescript library for calendar functionality (similar to Google Cal)? Looking to be able to show a monthly view with some events in it. Alternatively, is there a npm calendar that y'all recommend? Thank you!

Emi Gal22:04:52

Amazing, thank you @U8A5NMMGD! Big fan of your courses - I think I've done all of them! 🙂

3