0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-06 14:50:20 -05:00

Simplify time related types and remove momentjs dependency.

This commit is contained in:
Andrey Antukh 2017-03-06 18:40:49 +01:00
parent c4a5d26e8b
commit 9c4c267753
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
3 changed files with 12 additions and 84 deletions

View file

@ -24,7 +24,6 @@
[cljsjs/react-dom "15.4.2-2"] [cljsjs/react-dom "15.4.2-2"]
[cljsjs/react-dom-server "15.4.2-2"] [cljsjs/react-dom-server "15.4.2-2"]
[cljsjs/moment "2.17.1-0"]
[funcool/potok "2.0.0"] [funcool/potok "2.0.0"]
[funcool/struct "1.0.0"] [funcool/struct "1.0.0"]
[funcool/lentes "1.2.0"] [funcool/lentes "1.2.0"]

View file

@ -5,69 +5,14 @@
;; Copyright (c) 2015-2016 Andrey Antukh <niwi@niwi.nz> ;; Copyright (c) 2015-2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.util.time (ns uxbox.util.time
(:require [cljsjs.moment] (:require [vendor.datefns]
[cognitect.transit :as t])) [cognitect.transit :as t]))
;; --- Instant Impl (def ^:private dateFns js/dateFns)
(deftype Instant [^number v]
IComparable
(-compare [this other]
(if (instance? Instant other)
(compare v (.-v other))
(throw (js/Error. (str "Cannot compare " this " to " other)))))
IEquiv
(-equiv [this other]
(if (instance? Instant other)
(-equiv v (.-v other))
false))
IPrintWithWriter
(-pr-writer [_ writer _]
(let [m (js/moment v)
ms (.toISOString m)]
(-write writer (str "#instant \"" ms "\""))))
IHash
(-hash [_] v)
Object
(toString [this]
(let [m (js/moment v)]
(.toISOString m)))
(equiv [this other]
(-equiv this other)))
(defn instant
"Create a new Instant instance from
unix offset."
[v]
{:pre [(number? v)]}
(Instant. v))
(defn instant?
"Return true if `v` is an instance of Instant."
[v]
(instance? Instant v))
(defn parse
"Parse a string representation of instant
with an optional `format` parameter."
([v] (parse v :offset))
([v fmt]
(cond
(instant? v) v
(= fmt :offset) (Instant. v)
:else (let [m (if (= fmt :unix)
(js/moment.unix v)
(js/moment v fmt))]
(Instant. (.valueOf m))))))
(defn format (defn format
"Returns a string representation of the Instant "Returns a string representation of the Instant
instance with optional `fmt` format parameter. instace with optional `fmt` format parameter.
You can use `:iso` and `:unix` shortcuts as You can use `:iso` and `:unix` shortcuts as
format parameter. format parameter.
@ -77,32 +22,18 @@
" "
([v] (format v :iso)) ([v] (format v :iso))
([v fmt] ([v fmt]
{:pre [(instant? v)]} {:pre [(inst?? v)]}
(let [vm (js/moment (.-v v))] (case fmt
(case fmt :offset (.getTime v)
:unix (.unix vm) :iso (.format dateFns v)
:offset (.valueOf vm) (.format dateFns v fmt))))
:iso (.toISOString vm)
(.format vm fmt)))))
(defn now (defn now
"Return the current Instant." "Return the current Instant."
[] []
(let [vm (js/moment)] (js/Date.))
(Instant. (.valueOf vm))))
(defn timeago (defn timeago
[v] [v]
(let [dt (parse v) {:pre [(inst?? v)]}
vm (js/moment (.-v dt))] (.distanceInWordsToNow dateFns v))
(.fromNow vm)))
;; --- Transit Adapter
(def instant-write-handler
(t/write-handler
(constantly "m")
#(str (format % :offset))))
(def instant-read-handler
(t/read-handler #(instant (js/parseInt % 10))))

View file

@ -15,13 +15,11 @@
(def ^:privare +read-handlers+ (def ^:privare +read-handlers+
{"u" uuid {"u" uuid
"m" dt/instant-read-handler
"matrix" gmt/matrix-read-handler "matrix" gmt/matrix-read-handler
"point" gpt/point-read-handler}) "point" gpt/point-read-handler})
(def ^:privare +write-handlers+ (def ^:privare +write-handlers+
{dt/Instant dt/instant-write-handler {gmt/Matrix gmt/matrix-write-handler
gmt/Matrix gmt/matrix-write-handler
gpt/Point gpt/point-write-handler}) gpt/Point gpt/point-write-handler})
;; --- Public Api ;; --- Public Api