This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-11-15
Channels
- # aleph (24)
- # announcements (8)
- # babashka (27)
- # beginners (55)
- # biff (4)
- # calva (32)
- # cider (5)
- # clj-kondo (11)
- # clojure (59)
- # clojure-android (3)
- # clojure-australia (1)
- # clojure-belgium (6)
- # clojure-dev (21)
- # clojure-europe (26)
- # clojure-nl (1)
- # clojure-norway (17)
- # clojurescript (19)
- # css (1)
- # data-science (10)
- # datahike (17)
- # events (3)
- # figwheel-main (4)
- # honeysql (1)
- # hugsql (5)
- # hyperfiddle (1)
- # jobs (1)
- # leiningen (3)
- # lsp (6)
- # malli (5)
- # meander (4)
- # nbb (6)
- # off-topic (87)
- # pathom (19)
- # portal (2)
- # re-frame (4)
- # reitit (6)
- # releases (1)
- # remote-jobs (3)
- # shadow-cljs (29)
- # sql (8)
- # tools-deps (6)
- # xtdb (7)
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?
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
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]
Nevermind. Your`pr-str` suggestion gives exactly that. (Didn't try it immediately, since you said specific to defrecord/deftype.) Thank you.
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 stillAccording 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
Could it be that unparse-local
is needed?
http://www.andrewmcveigh.com/cljs-time/latest/cljs-time.format.html#var-unparse-local
I tried that too
(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))))
(.toLocaleString (js/Date.) #js [] #js {:timeZone "CET"
:hour "numeric"
:minute "numeric"
:second "numeric"
:timeZoneName "short"})
Can I do conditional require’ing in clojurescript? eg make the :require
form depends on some goog-define
constant?
I don’t understand your answer. What are class paths in that context?
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.@U2FRKM4TW just a quick aditional question, will only required namespaces/node packages be included in my build?
Awesome, thanks.