Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

don't use extend-type on JS natives for IEncodeJS IEncodeClojure

  • Loading branch information...
commit 9ac90619285e8a40bffe908a74031e469ddc9072 1 parent 00e1575
@swannodette swannodette authored
Showing with 46 additions and 41 deletions.
  1. +46 −41 src/cljs/cljs/core.cljs
View
87 src/cljs/cljs/core.cljs
@@ -6786,65 +6786,70 @@ reduces them without incurring seq initialization"
(-key->js [x] "Transforms map keys to valid JavaScript keys. Arbitrary keys are
encoded to their string representation via (pr-str x)"))
-(extend-protocol IEncodeJS
- default
- (-key->js [k]
+(declare clj->js)
+
+(defn key->js [k]
+ (if (satisfies? IEncodeJS k)
+ (-clj->js k)
(if (or (string? k)
(number? k)
(keyword? k)
(symbol? k))
- (-clj->js k)
- (pr-str k)))
-
- (-clj->js [x]
- (cond
- (keyword? x) (name x)
- (symbol? x) (str x)
- (map? x) (let [m (js-obj)]
- (doseq [[k v] x]
- (aset m (-key->js k) (-clj->js v)))
- m)
- (coll? x) (apply array (map -clj->js x))
- :else x))
-
- nil
- (-clj->js [x] nil))
+ (clj->js k)
+ (pr-str k))))
(defn clj->js
"Recursively transforms ClojureScript values to JavaScript.
sets/vectors/lists become Arrays, Keywords and Symbol become Strings,
Maps become Objects. Arbitrary keys are encoded to by key->js."
[x]
- (-clj->js x))
+ (when-not (nil? x)
+ (if (satisfies? IEncodeJS x)
+ (-clj->js x)
+ (cond
+ (keyword? x) (name x)
+ (symbol? x) (str x)
+ (map? x) (let [m (js-obj)]
+ (doseq [[k v] x]
+ (aset m (key->js k) (clj->js v)))
+ m)
+ (coll? x) (apply array (map clj->js x))
+ :else x))))
(defprotocol IEncodeClojure
- (-js->clj [x] [x options] "Transforms JavaScript values to Clojure"))
-
-(extend-protocol IEncodeClojure
- default
- (-js->clj
- ([x options]
- (let [{:keys [keywordize-keys]} options
- keyfn (if keywordize-keys keyword str)
- f (fn thisfn [x]
- (cond
- (seq? x) (doall (map thisfn x))
- (coll? x) (into (empty x) (map thisfn x))
- (goog.isArray x) (vec (map thisfn x))
- (identical? (type x) js/Object) (into {} (for [k (js-keys x)]
- [(keyfn k)
- (thisfn (aget x k))]))
- :else x))]
- (f x)))
- ([x] (-js->clj x {:keywordize-keys false}))))
+ (-js->clj [x options] "Transforms JavaScript values to Clojure"))
(defn js->clj
"Recursively transforms JavaScript arrays into ClojureScript
vectors, and JavaScript objects into ClojureScript maps. With
option ':keywordize-keys true' will convert object fields from
strings to keywords."
- [x & opts]
- (-js->clj x (apply array-map opts)))
+ ([x] (js->clj x {:keywordize-keys false}))
+ ([x & opts]
+ (cond
+ (satisfies? x IEncodeClojure)
+ (-js->clj x (apply array-map opts))
+
+ (seq opts)
+ (let [{:keys [keywordize-keys]} opts
+ keyfn (if keywordize-keys keyword str)
+ f (fn thisfn [x]
+ (cond
+ (seq? x)
+ (doall (map thisfn x))
+
+ (coll? x)
+ (into (empty x) (map thisfn x))
+
+ (goog.isArray x)
+ (vec (map thisfn x))
+
+ (identical? (type x) js/Object)
+ (into {} (for [k (js-keys x)]
+ [(keyfn k) (thisfn (aget x k))]))
+
+ :else x))]
+ (f x)))))
(defn memoize
"Returns a memoized version of a referentially transparent function. The
Please sign in to comment.
Something went wrong with that request. Please try again.