Minor refactor on geom types and corresponding transit handlers.

This commit is contained in:
Andrey Antukh 2017-02-14 21:56:15 +01:00
parent b15d70ff91
commit 340a5b3da2
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
5 changed files with 89 additions and 95 deletions

View file

@ -5,9 +5,10 @@
;; Copyright (c) 2015-2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.util.time
(:require [cljsjs.moment]))
(:require [cljsjs.moment]
[cognitect.transit :as t]))
;; A simplified immutable date time representation type.
;; --- Instant Impl
(deftype Instant [^number v]
IComparable
@ -16,24 +17,28 @@
(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))
IHash
(-hash [_] v))
(alter-meta! #'->Instant assoc :private true)
(-equiv this other)))
(defn instant
"Create a new Instant instance from
@ -92,12 +97,12 @@
vm (js/moment (.-v dt))]
(.fromNow vm)))
;; (defn day
;; [v]
;; (let [dt (parse v)
;; vm (js/moment (.-v dt))
;; fmt #js {:sameDay "[Today]"
;; :sameElse "[Today]"
;; :lastDay "[Yesterday]"
;; :lastWeek "[Last] dddd"}]
;; (.calendar vm nil fmt)))
;; --- 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))))