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?
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.
cljx has been replaced by reader conditionals in core
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?
.cljc
is the new .cljx
I find many libs are cross platform now. Maybe not ones that are mainly wrappers.
Instaparse seems to support cljs as well
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?
Seems like it really should be a non-env specific library.
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
ok cool. so there’s no canonical json parsing lib that works on both JS and JVM?
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=>
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
not that i'm aware of. because on js json parsing is native
ah that makes sense. I hadn’t thought about that aspect.
Thanks for catching me up. I’ll just reader conditional the parsing steps.
(defn read-json-str [s] #?(:clj (jsonista/read-value s), :cljs (.parse js/JSON s)))
Or whateverbeware 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
Oops sorry about that
(defn read-json-str [s] #?(:clj (jsonista/read-value s), :cljs (js->clj (.parse js/JSON s))))
And all the JVM JSON libs and js->clj
support keywordizing keys but a bit differently
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.
Oh, and this: (identical? (type x) js/Object)
. So if your JSON data has fields named constructor
, js->clj
will produce incorrect results.
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!
Probably these two is something you could explore: • npm: https://github.com/jquense/react-big-calendar • vanilla cljs: http://metosin.github.io/komponentit/#!/example.calendar
Amazing, thank you @jacek.schae! Big fan of your courses - I think I've done all of them! 🙂