From 9c4c26775318404c8a679d187ed0a0ce904350d9 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 6 Mar 2017 18:40:49 +0100 Subject: [PATCH] Simplify time related types and remove momentjs dependency. --- frontend/project.clj | 1 - frontend/src/uxbox/util/time.cljs | 91 ++++------------------------ frontend/src/uxbox/util/transit.cljs | 4 +- 3 files changed, 12 insertions(+), 84 deletions(-) diff --git a/frontend/project.clj b/frontend/project.clj index 8bfdce0b5..6373f108a 100644 --- a/frontend/project.clj +++ b/frontend/project.clj @@ -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"] diff --git a/frontend/src/uxbox/util/time.cljs b/frontend/src/uxbox/util/time.cljs index 6e758f5d4..d7b21b585 100644 --- a/frontend/src/uxbox/util/time.cljs +++ b/frontend/src/uxbox/util/time.cljs @@ -5,69 +5,14 @@ ;; Copyright (c) 2015-2016 Andrey Antukh (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)) diff --git a/frontend/src/uxbox/util/transit.cljs b/frontend/src/uxbox/util/transit.cljs index 4d1e5030d..d68d19a01 100644 --- a/frontend/src/uxbox/util/transit.cljs +++ b/frontend/src/uxbox/util/transit.cljs @@ -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