~sjm/redwood

ref: de4c8b31042635c5d5f635d9b8f4469b55933399 redwood/src/redwood/data/journal.cljs -rw-r--r-- 825 bytes
de4c8b31Sam Marshall save journal to localstorage 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(ns redwood.data.journal
  (:require [clojure.spec.alpha :as s]
            [cljs-time.core :as t]
            [redwood.data.time :as dt]))

(s/def ::time t/date?)

(s/def ::contents string?)

(s/def ::entry
  (s/keys :req [::time ::contents]))

(defn create-entry
  [time contents]
  {::time time
   ::contents contents})

(defn entry-js->clj
  [{:keys [:time :contents]}]
  (create-entry 
    (dt/string->time time)
    contents))

(defn journal->string 
  [journal]
  (->> journal
      (map (fn [{:keys [::time ::contents]}]
             {:time (dt/time->string time)
              :contents contents}))
      (clj->js)
      (.stringify js/JSON)))

(defn string->journal
  [string]
  (let [parse #(js->clj % :keywordize-keys true)]
    (->> string
        (.parse js/JSON)
        (parse)
        (map entry-js->clj))))