Skip to content

Commit

Permalink
Adds more date helpers and fixes cljs require bug
Browse files Browse the repository at this point in the history
  • Loading branch information
oconn authored and dehli committed Nov 11, 2019
1 parent 4b91124 commit 31b6764
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>dehli</groupId>
<artifactId>serverless</artifactId>
<version>0.1.4</version>
<version>0.1.5</version>
<name>serverless</name>

<dependencies>
Expand Down
13 changes: 7 additions & 6 deletions src/serverless/core/async.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
(ns serverless.core.async)
(ns serverless.core.async
(:require [cljs.core.async :as async]))

(defmacro go-try
[& body]
`(let [chan# (cljs.core.async/promise-chan)]
(cljs.core.async/go
`(let [chan# (async/promise-chan)]
(async/go
(let [result# (try ~@body (catch :default e# e#))]
(if (nil? result#)
(cljs.core.async/close! chan#)
(cljs.core.async/>! chan# result#))))
(async/close! chan#)
(async/>! chan# result#))))
chan#))

(defmacro <? [ch]
`(let [val# (cljs.core.async/<! ~ch)]
`(let [val# (async/<! ~ch)]
(if (instance? js/Error val#)
(throw val#)
val#)))
Expand Down
1 change: 1 addition & 0 deletions src/serverless/core/async.cljs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
(ns serverless.core.async
(:require [cljs.core.async])
(:require-macros [serverless.core.async]))
34 changes: 33 additions & 1 deletion src/serverless/date.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
(ns serverless.date)

(defn now []
(defn now
"Milliseconds since epoch (now)"
[]
(js-invoke js/Date "now"))

(defn ->iso-str
"Converts datetime object into ISO string"
[datetime-obj]
(js-invoke (new js/Date datetime-obj) "toISOString"))

(defn now-iso-str
"Now ISO string"
[]
(->iso-str (now)))

(defn ->timestamp
"Converts any format-table js datetime object to milliseconds from epoch"
[datetime-object]
(js-invoke (new js/Date datetime-object) "getTime"))

(defn before?
"t1 is before t2"
[t1 t2]
(< (->timestamp t1) (->timestamp t2)))

(defn after?
"t1 is after t2"
[t1 t2]
(> (->timestamp t1) (->timestamp t2)))

(defn equal?
"t1 is equal to t2"
[t1 t2]
(= (->timestamp t1) (->timestamp t2)))

0 comments on commit 31b6764

Please sign in to comment.