Skip to content

Instantly share code, notes, and snippets.

@jacobemcken
Last active January 3, 2022 22:52
Show Gist options
  • Save jacobemcken/82d5df5822a44c5ed646182515c9ffb7 to your computer and use it in GitHub Desktop.
Save jacobemcken/82d5df5822a44c5ed646182515c9ffb7 to your computer and use it in GitHub Desktop.
ClojureScript (cljs) Transit Day.js
(ns app.transit-format
(:require ["dayjs" :as dayjs]
["dayjs/plugin/duration" :as duration]
[cognitect.transit :as transit]
[haslett.format :as fmt]))
;; Extend Day.js with more functionality
(.extend dayjs duration)
(def duration-type
"Dummy \"duration\" type, because the Day.js lib doesn't provide one."
(type (dayjs/duration 1)))
(def write-handlers
{duration-type (transit/write-handler (fn [] "time/duration")
(fn [duration]
(.toISOString duration)))})
(def read-handlers
{"time/duration" (transit/read-handler #(dayjs/duration %))})
(def custom
"Read and write data encoded in transit+json."
(reify fmt/Format
(read [_ s] (transit/read (transit/reader :json {:handlers read-handlers}) s))
(write [_ v] (transit/write (transit/writer :json {:handlers write-handlers}) v))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment