clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
Mario C. 2021-04-21T15:04:34.351100Z

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?

bwstearns 2021-04-21T15:56:00.353Z

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.

nilern 2021-04-21T15:57:33.353200Z

cljx has been replaced by reader conditionals in core

bwstearns 2021-04-21T15:58:25.353400Z

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?

nilern 2021-04-21T15:58:59.353600Z

.cljc is the new .cljx

nilern 2021-04-21T16:00:25.353800Z

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

nilern 2021-04-21T16:01:11.354Z

Instaparse seems to support cljs as well

bwstearns 2021-04-21T16:03:47.354200Z

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?

bwstearns 2021-04-21T16:04:22.354400Z

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

dpsutton 2021-04-21T16:07:23.354600Z

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

bwstearns 2021-04-21T16:10:32.354900Z

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

dpsutton 2021-04-21T16:10:33.355100Z

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=>

nilern 2021-04-21T16:10:51.355300Z

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

dpsutton 2021-04-21T16:10:53.355500Z

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

bwstearns 2021-04-21T16:11:24.355700Z

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

bwstearns 2021-04-21T16:11:57.355900Z

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

👍 1
nilern 2021-04-21T16:12:48.356200Z

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

👍 1
dpsutton 2021-04-21T16:15:10.356500Z

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

nilern 2021-04-21T16:15:40.356700Z

Oops sorry about that

nilern 2021-04-21T16:16:01.356900Z

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

nilern 2021-04-21T16:20:26.357100Z

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

p-himik 2021-04-21T16:51:50.357300Z

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-himik 2021-04-21T16:53:18.357500Z

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

2021-04-21T18:59:28.358800Z

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!

jacekschae 2021-04-21T19:21:06.359100Z

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

2021-04-21T22:55:52.359500Z

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

1