0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-04 13:50:12 -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-server "15.4.2-2"]
[cljsjs/moment "2.17.1-0"]
[funcool/potok "2.0.0"]
[funcool/struct "1.0.0"]
[funcool/lentes "1.2.0"]

View file

@ -5,69 +5,14 @@
;; Copyright (c) 2015-2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.util.time
(:require [cljsjs.moment]
(:require [vendor.datefns]
[cognitect.transit :as t]))
;; --- Instant Impl
(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))))))
(def ^:private dateFns js/dateFns)
(defn format
"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
format parameter.
@ -77,32 +22,18 @@
"
([v] (format v :iso))
([v fmt]
{:pre [(instant? v)]}
(let [vm (js/moment (.-v v))]
(case fmt
:unix (.unix vm)
:offset (.valueOf vm)
:iso (.toISOString vm)
(.format vm fmt)))))
{:pre [(inst?? v)]}
(case fmt
:offset (.getTime v)
:iso (.format dateFns v)
(.format dateFns v fmt))))
(defn now
"Return the current Instant."
[]
(let [vm (js/moment)]
(Instant. (.valueOf vm))))
(js/Date.))
(defn timeago
[v]
(let [dt (parse v)
vm (js/moment (.-v dt))]
(.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))))
{:pre [(inst?? v)]}
(.distanceInWordsToNow dateFns v))

View file

@ -15,13 +15,11 @@
(def ^:privare +read-handlers+
{"u" uuid
"m" dt/instant-read-handler
"matrix" gmt/matrix-read-handler
"point" gpt/point-read-handler})
(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})
;; --- Public Api