Skip to content

Commit

Permalink
Sort out CLJS kinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Truter committed Jun 5, 2016
1 parent 2bc047d commit 51dc48f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/matchbox/serialization/plain.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
(:require
[clojure.walk :as walk]
[matchbox.utils :as utils])
(:import
(java.util HashMap ArrayList)))
#?(:clj (:import (java.util HashMap ArrayList))))

(defn hydrate-raw [x]
#?(:cljs x
Expand Down
12 changes: 5 additions & 7 deletions src/matchbox/serialization/sorted.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
[clojure.walk :as walk]
[matchbox.serialization.keyword :as keyword]
[matchbox.utils :as utils])
(:import
(java.util HashMap ArrayList)
(clojure.lang PersistentTreeMap)))
#?(:clj (:import (java.util HashMap ArrayList))))

(defn map->sorted [m]
(if (instance? PersistentTreeMap m) m (into (sorted-map) (map (juxt (comp keyword key) val) m))))
(if (sorted? m) m (into (sorted-map) (map (juxt (comp keyword key) val) m))))

(defn hydrate-shallow [x]
(cond
#?@(:clj [(instance? HashMap x) (recur (map->sorted x))
(instance? ArrayList x) (recur (into [] x))])
(map? x) (map->sorted x)
#?@(:cljs [(array? x) (vec x)
(identical? js/Object (type x)) (into (sorted-map) (for [k (js-keys x)] [(keyword k) (aget x k)]))])
:else (keyword/hydrate-kw x)))

(defn hydrate [v]
#?(:clj (walk/prewalk hydrate-shallow v)
:cljs (walk/postwalk hydrate-shallow (js->clj v))))
(defn hydrate [v] (walk/prewalk hydrate-shallow v))

(def serialize keyword/serialize)

Expand Down
10 changes: 4 additions & 6 deletions src/matchbox/serialization/stable.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
[matchbox.serialization.keyword :as keyword]
[linked.map]
[matchbox.utils :as utils])
(:import
(java.util HashMap ArrayList)
(clojure.lang PersistentTreeMap)))
#?(:clj (:import (java.util HashMap ArrayList))))

(defn map->linked [m]
(if (instance? linked.map.LinkedMap m) m (linked.map/->linked-map (map (juxt (comp keyword key) val) m))))
Expand All @@ -16,11 +14,11 @@
#?@(:clj [(instance? HashMap x) (recur (map->linked x))
(instance? ArrayList x) (recur (into [] x))])
(map? x) (map->linked x)
#?@(:cljs [(array? x) (vec x)
(identical? js/Object (type x)) (linked.map/->linked-map (for [k (js-keys x)] [(keyword k) (aget x k)]))])
:else (keyword/hydrate-kw x)))

(defn hydrate [v]
#?(:clj (walk/prewalk hydrate-shallow v)
:cljs (walk/postwalk hydrate-shallow (js->clj v))))
(defn hydrate [v] (walk/prewalk hydrate-shallow v))

(def serialize keyword/serialize)

Expand Down
9 changes: 4 additions & 5 deletions test/matchbox/serialization_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
[matchbox.serialization.keyword :as keyword]
[matchbox.serialization.sorted :as sorted]
[matchbox.serialization.stable :as stable])
(:import
(java.util HashMap)))
#?(:clj (:import (java.util HashMap))))

;; serialize / deserialize (specs)

Expand Down Expand Up @@ -105,11 +104,11 @@

(def ref-map
(reduce
(fn [#?(:clj ^HashMap acc :cljs (js/Object.)) [k v]]
#?(:clj (.put acc k v) :cjls (aset acc k v))
(fn [#?(:clj ^HashMap acc :cljs acc) [k v]]
#?(:clj (.put acc k v) :cljs (aset acc k v))
acc)
(let [base {"a" {"b" {"c" [{"d" "e" "f" ":f"}]}}}]
#?(:clj (HashMap. base) :cjls (clj->js base)))
#?(:clj (HashMap. base) :cljs (clj->js base)))
(map vector (map (comp str char) (range 98 123)) (range 25))))

(deftest custom-serializers-test
Expand Down

0 comments on commit 51dc48f

Please sign in to comment.