Fork me on GitHub
#clojurescript
<
2022-11-15
>
skylize03:11:39

In clj, I can use (.getName (class foo)) to get a likely-unique string with at least some meanginful context usually embedded in the name. Is there something at all similar in cljs?

thheller06:11:43

depends on what foo is. JS doesn't have named classes as java has. if its an instance of a defrecord/deftype (pr-str (type foo)) is probably closest

skylize15:11:10

If I def a function, it gets some type of data attached to it that shows in the repl when evaluating a symbol that points to it. How do I get to that as a string?

(ns my.namespace)

(defn foo [])
(def bar foo)

foo
;; => #object [my$namespace$foo]
bar
;; => #object [my$namespace$foo]

skylize15:11:08

Nevermind. Your`pr-str` suggestion gives exactly that. (Didn't try it immediately, since you said specific to defrecord/deftype.) Thank you.

martinklepsch15:11:54

Hey! I’m trying to format a date for a specific timezone but somehow I’m just always getting the UTC format:

(require '[cljs-time.format :as tfmt]
          '[cljs-time.core :as time])
 (def d (time/date-time 2022 07 15 5 30))
 (tfmt/unparse (tfmt/formatter "HH:mm" (time/time-zone-for-offset 2)) d)
 ;; => "05:30"
My goal is to show this in CEST time (e.g. +2). I tried a bunch of variations but I think I’m missing something still

grav17:11:29

According to docs for unparse, it "Returns a string representing the given DateTime instance in UTC" http://www.andrewmcveigh.com/cljs-time/latest/cljs-time.format.html#var-unparse

grav17:11:53

Not sure why the timezone is then needed at all for the formatter ...

martinklepsch17:11:14

I tried that too

martinklepsch17:11:01

(defn user-timezone
  []
  (or (.. (js/Intl.DateTimeFormat) resolvedOptions -timeZone)
      (tzd/detectTimeZone)))

(defn format-date-to-tz
  ([date tz]
   (format-date-to-tz date tz [:hour :minute]))
  ([date tz opts]
   (.. (js/Intl.DateTimeFormat. "de-DE"
                                (clj->js (merge {:timeZone tz}
                                                (select-keys {:weekday "short"
                                                              :hour "numeric"
                                                              :minute "numeric"
                                                              :day "numeric"
                                                              :month "short"
                                                              :year "numeric"}
                                                             opts))))
       (format date))))

Brandon Stubbs01:11:53

(.toLocaleString (js/Date.) #js [] #js {:timeZone     "CET"
                                          :hour         "numeric"
                                          :minute       "numeric"
                                          :second       "numeric"
                                          :timeZoneName "short"})

👍 1
Quentin Le Guennec22:11:49

Can I do conditional require’ing in clojurescript? eg make the :require form depends on some goog-define constant?

p-himik22:11:50

No, but you can have conditional class paths.

Quentin Le Guennec07:11:51

I don’t understand your answer. What are class paths in that context?

p-himik07:11:20

It's the same thing regardless of the context. It's a JVM thing, but it's of great importance to both Clojure and ClojureScript. In this particular case, the stuff in your :paths (assuming you're using deps.edn) is on the class path. Instead of using a compile-time constant, you can use a compile-time flag (namely, an alias) to add a different thing to the class path with the same namespace. So suppose your deps.edn has something like:

{:paths   ["src"]
 :deps    {...}
 :aliases {:prod {:extra-paths ["prod"]}
           :dev  {:extra-paths ["dev"]}}}
And your project structure looks like this:
root
- src
  - app
    - core.cljs
- prod
  - app
    - stuff.cljs
- dev
  - app
    - stuff.cljs
And src/app/core.cljs is
(ns app.core
  (:require [app.stuff :as s]))

(js/console.log s/value)
Where prod/app/stuff.cljs is
(ns app.stuff)

(def value "prod")
And dev/app/stuff.cljs is
(ns app.stuff)

(def value "dev")
Now, if you compile and run app.core with the :prod alias, it will print out prod. And for the :dev alias, it will print out dev. To simulate conditional :require, you can add different requires to different files that are under the same (class path-dependent) namespace.

Quentin Le Guennec07:11:13

oh, I understand, thanks a lot.

👍 1
Quentin Le Guennec08:11:00

@U2FRKM4TW just a quick aditional question, will only required namespaces/node packages be included in my build?

p-himik08:11:15

Of course.