Skip to content

Commit 812e1eb

Browse files
committed
format ns loading and usable on node and jvm
1 parent f6c34c7 commit 812e1eb

File tree

9 files changed

+144
-68
lines changed

9 files changed

+144
-68
lines changed

cljs/java_time/util.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns java-time.util)
2+
3+
(defmacro static-prop [target prop]
4+
`(goog.object/get ~target (str ~prop)))
5+
6+
(defmacro static-call [target prop & args]
7+
`((goog.object/get ~target (str ~prop)) ~@args))
8+
9+

dev/cljs_repl.clj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(ns cljs-repl)
2+
3+
(require
4+
'[cljs.repl :as cljs-repl]
5+
'[cljs.repl.node :as node])
6+
7+
(cljs-repl/repl* (node/repl-env)
8+
{:output-dir "out"
9+
:optimizations :none
10+
:cache-analysis true
11+
:source-map true})
12+
13+

project.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
:url "http://opensource.org/licenses/MIT"}
66
:scm {:name "git"
77
:url "http://github.com/dm3/clojure.java-time"}
8-
:dependencies [[clj-tuple "0.2.2"]]
8+
:dependencies [[clj-tuple "0.2.2"]
9+
[cljsjs/js-joda "1.6.2-0"]
10+
[org.clojure/clojurescript "1.9.946"]]
11+
:source-paths ["src" "cljs"]
912
:profiles {:dev {:dependencies [[org.clojure/clojure "1.8.0"]
1013
[criterium "0.4.4"]
1114
[com.taoensso/timbre "4.1.4"]

src/java_time/core.clj renamed to src/java_time/core.cljc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
(ns java-time.core
22
(:refer-clojure :exclude (zero? range max min))
3-
(:import [java.time.temporal ValueRange]
4-
[java.time.chrono Chronology]))
3+
#?(:clj
4+
(:import [java.time.temporal ValueRange]
5+
[java.time.chrono Chronology])
6+
:cljs (:require [cljsjs.js-joda])
7+
))
8+
9+
#?( :cljs (def ValueRange (.. js/JSJoda -ValueRange)))
10+
#?( :cljs (def Chronology (.. js/JSJoda -IsoChronology)))
11+
#?( :cljs (def Number js/Number))
12+
13+
514

615
(defprotocol Amount
716
(zero? [a]

src/java_time/format.clj renamed to src/java_time/format.cljc

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@
22
(:refer-clojure :exclude (format))
33
(:require [clojure.string :as string]
44
[java-time.core :as jt.c]
5-
[java-time.util :as jt.u])
6-
(:import [java.time.temporal TemporalAccessor]
7-
[java.time.format DateTimeFormatter DateTimeFormatterBuilder ResolverStyle]))
5+
[java-time.util :as jt.u :include-macros true])
6+
#?(:clj (:import [java.time.temporal TemporalAccessor]
7+
[java.time.format DateTimeFormatter DateTimeFormatterBuilder ResolverStyle])))
8+
9+
#?(:cljs
10+
(do
11+
(def DateTimeFormatter (.. js/JSJoda -DateTimeFormatter))
12+
(def ResolverStyle (.. js/JSJoda -ResolverStyle))))
13+
14+
15+
(def formatter-fields
16+
#?(:clj (jt.u/get-static-fields-of-type DateTimeFormatter DateTimeFormatter)
17+
:cljs (jt.u/get-fields-matching DateTimeFormatter #"ISO.*")))
818

919
(def predefined-formatters
10-
(->> (jt.u/get-static-fields-of-type DateTimeFormatter DateTimeFormatter)
20+
(->> formatter-fields
1121
(jt.u/map-kv
1222
(fn [^String n fmt]
1323
[(string/lower-case (.replace n \_ \-)) fmt]))))
1424

1525
(defn- get-resolver-style [s]
1626
(if (instance? ResolverStyle s) s
1727
(case s
18-
:strict ResolverStyle/STRICT
19-
:smart ResolverStyle/SMART
20-
:lenient ResolverStyle/LENIENT)))
28+
:strict (jt.u/static-prop ResolverStyle 'STRICT)
29+
:smart (jt.u/static-prop ResolverStyle 'SMART)
30+
:lenient (jt.u/static-prop ResolverStyle 'LENIENT))))
2131

2232
(defn ^DateTimeFormatter formatter
2333
"Constructs a DateTimeFormatter out of a
@@ -33,7 +43,7 @@
3343
([fmt {:keys [resolver-style]}]
3444
(let [^DateTimeFormatter fmt
3545
(cond (instance? DateTimeFormatter fmt) fmt
36-
(string? fmt) (DateTimeFormatter/ofPattern fmt)
46+
(string? fmt) (jt.u/static-call DateTimeFormatter 'ofPattern fmt)
3747
:else (get predefined-formatters (name fmt)))
3848
fmt (if resolver-style
3949
(.withResolverStyle fmt (get-resolver-style resolver-style))
File renamed without changes.

src/java_time/util.clj

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/java_time/util.cljc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
(ns java-time.util
2+
(:require [clojure.string :as string]
3+
#?@(:clj
4+
[[java-time.util-macros]
5+
[java-time.potemkin.namespaces :as potemkin]]))
6+
#?(:clj
7+
(:import [java.lang.reflect Field])))
8+
9+
#?(:clj
10+
(potemkin/import-vars
11+
[java-time.util-macros static-prop static-call]))
12+
13+
#?(:clj
14+
(defn get-static-fields-of-type [^Class klass, ^Class of-type]
15+
(->> (seq (.getFields klass))
16+
(map (fn [^Field f]
17+
(when (.isAssignableFrom of-type (.getType f))
18+
[(.getName f) (.get f nil)])))
19+
(keep identity)
20+
(into {})))
21+
:cljs
22+
(defn get-fields-matching [obj regex]
23+
(->>
24+
(js-keys obj)
25+
(filter #(re-matches regex %))
26+
(map #(vector % (goog.object/get obj %)))
27+
(into {}))))
28+
29+
(defn dashize [camelcase]
30+
(let [words (re-seq #"([^A-Z]+|[A-Z]+[^A-Z]*)" camelcase)]
31+
(string/join "-" (map (comp string/lower-case first) words))))
32+
33+
34+
#?(:clj
35+
(defmacro if-threeten-extra [then-body else-body]
36+
(if (try (Class/forName "org.threeten.extra.Temporals")
37+
(catch Throwable e))
38+
`(do ~then-body)
39+
`(do ~else-body)))
40+
:cljs (defn if-threeten-extra [] false))
41+
42+
#?(:clj
43+
(defmacro when-threeten-extra [& body]
44+
(if (try (Class/forName "org.threeten.extra.Temporals")
45+
(catch Throwable e))
46+
`(do ~@body)))
47+
:cljs (defn when-threeten-extra []))
48+
49+
#?(:clj
50+
(defmacro when-joda-time-loaded
51+
"Execute the `body` when Joda-Time classes are found on the classpath.
52+
53+
Take care - when AOT-compiling code using this macro, the Joda-Time classes
54+
must be on the classpath at compile time!"
55+
[& body]
56+
(if (try (Class/forName "org.joda.time.DateTime")
57+
(catch Throwable e))
58+
`(do ~@body)))
59+
:cljs (defn when-joda-time-loaded []))
60+
61+
;; From Medley, C Weavejester
62+
(defn editable? [coll]
63+
#?(:clj (instance? clojure.lang.IEditableCollection coll)
64+
:cljs (satisfies? cljs.core.IEditableCollection coll)))
65+
66+
(defn reduce-map [f coll]
67+
(if (editable? coll)
68+
(persistent! (reduce-kv (f assoc!) (transient (empty coll)) coll))
69+
(reduce-kv (f assoc) (empty coll) coll)))
70+
71+
(defn map-vals
72+
"Maps a function over the values of an associative collection."
73+
[f coll]
74+
(reduce-map (fn [xf] (fn [m k v] (xf m k (f v)))) coll))
75+
76+
(defn map-kv
77+
"Maps a function over the key/value pairs of an associate collection. Expects
78+
a function that takes two arguments, the key and value, and returns the new
79+
key and value as a collection of two elements."
80+
[f coll]
81+
(reduce-map (fn [xf] (fn [m k v] (let [[k v] (f k v)] (xf m k v)))) coll))

src/java_time/util_macros.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(ns java-time.util-macros)
2+
3+
(defmacro static-prop [target prop]
4+
`(. ~target ~(second prop)))
5+
6+
(defmacro static-call [target prop & args]
7+
`(. ~target ~(second
8+
prop) ~@args))

0 commit comments

Comments
 (0)