Fork me on GitHub
#clojurescript
<
2020-12-03
>
leif02:12:19

So, I was looking at this question on SO: https://stackoverflow.com/questions/51121526/how-to-customise-the-print-function-for-native-objects-in-clojurescript And I was wondering if there's any good documentation on helping me to understand how extend-protocol and IPrintWithWriter works. The official documentation is a bit too...terse. Basically, I'm first trying to understand what object is doing, as I thought a type needed to go there.

lilactown02:12:29

There are several built in symbols that can be extended - object, number, string

lilactown02:12:34

There might be more

lilactown02:12:24

For some reason you should extend these symbols instead of the type - e.g. js/Object - I don’t remember why

leif03:12:25

Odd...alright then. Thanks. 🙂

leif03:12:59

Next, do you know why they used write-all instead of -write?

leif03:12:27

As in this call:

(extend-protocol IPrintWithWriter
        js/Symbol
        (-pr-writer [sym writer _]
          (-write writer (str "\"" (.toString sym) "\""))))

leif03:12:40

Oh, I guess I should tag @U4YGF4NGM

greensponge13:12:30

Hi everyone, I have a CLJS project using lein and shadow-cljs and I'm trying to learn spec. What is it I have to do to get generate to work in the REPL? I've set these: 1. Dev dependency on [org.clojure/test.check] in project.clj 2. Requiring [cljs.spec.alpha :as s] and [cljs.spec.gen.alpha :as gen] in the dev/repl namespace cljs.user 3. In the REPL I am inside the cljs.user namespace and everything except gen works as expected If I run some (s/def) and check those with the s alias, that works fine! But if I try to use gen like in the documentation: (gen/generate (s/gen int?)) I get this error->

#object[Error Error: Var clojure.test.check.generators/simple-type-printable does not exist, clojure.test.check.generators never required]
What am I missing? Does this only work in clojure and not in clojurescript?

greensponge13:12:03

I should add that I've tried requiring all kinds of different variations and checked other similar reports on this error, most say that it goes away with what I did in step #1, but for me it doesn't.

jaime13:12:38

When using cljs.test, is there a way to provide test data that will be used inside test method? I'm doing something below, but seems like shadow-cljs :browser-test can't detect the test

(deftest "add"
  (for [[x y sum] [[1 2 3]
                   [1 1 2]
                   [1 5 6]]]
   (testing "should add two numbers"
     (is (= sum (add x y))))))

borkdude13:12:37

@jaime.sangcap first, you should write (deftest add ...). Furthermore, you should probably use doseq instead of for since for produces a lazy seq and isn't intended for side effects like testing.

3
jaime13:12:34

OMG it worked! Thanks a lot 😄

tzzh15:12:05

Hey, how can I call cljs from node js ? I have some node code that needs to convert js objects to edn and I was wondering if I could call js->edn from node (and more generally if I could write node modules in cljs and then use them from node)

tzzh15:12:14

thanks :thumbsup:

GGfpc19:12:05

Hello, not sure if this is the right channel, but I'm trying to generate a javascript extern to use with reagent. I'm following this guide from cljsjs: https://github.com/cljsjs/packages/wiki/Creating-Externs And this is the library: https://github.com/weknowinc/react-bubble-chart-d3 The thing is, when I try to use the extern generator (http://jmmk.github.io/javascript-externs-generator/) I add my dist file (https://cdn.jsdelivr.net/gh/weknowinc/react-bubble-chart-d3@0eaf1ecef5c0f7c9c8eba4d779e18cfeb10884ba/dist/react-bubble-chart-d3.js) but when I input the javascript object name (which I assume is BubbleChart) it tells me that the namespace cannot be found. Can anyone tell me what I'm doing wrong?

p-himik20:12:02

I'm not sure what exactly is wrong, but there's no externs inferences, specifically so you don't have to generate externs in advance. Just add ^js in front of every variable that has a JS object at the point of its declaration - it should do the trick.

(defn do-stuff [^js val]
  ;; `prop` will not be manged by optimizations.
  (println (.-prop val)))

p-himik20:12:01

Same reason why CLJSJS is now basically obsolete - you can use NPM packages just as they are, without having to undergo extra steps. It's especially easy if you use shadow-cljs.

p-himik20:12:03

Some additional info: https://shadow-cljs.github.io/docs/UsersGuide.html#externs There are also guides out there that aren't specific to shadow-cljs.