Skip to content

Commit

Permalink
CLJS-2958 - make symbol work on keywords and vars
Browse files Browse the repository at this point in the history
  • Loading branch information
souenzzo authored and mfikes committed Dec 6, 2018
1 parent e523cfa commit b38ad7e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,16 +1098,24 @@ defaults to returning v."))
IPrintWithWriter
(-pr-writer [o writer _] (-write writer str)))

(defn var?
"Returns true if v is of type cljs.core.Var"
[v]
(instance? cljs.core.Var v))

(defn symbol
"Returns a Symbol with the given namespace and name."
"Returns a Symbol with the given namespace and name. Arity-1 works
on strings, keywords, and vars."
([name]
(if (symbol? name)
name
(let [idx (.indexOf name "/")]
(if (< idx 1)
(symbol nil name)
(symbol (.substring name 0 idx)
(.substring name (inc idx) (. name -length)))))))
(cond (symbol? name) name
(string? name) (let [idx (.indexOf name "/")]
(if (< idx 1)
(symbol nil name)
(symbol (.substring name 0 idx)
(.substring name (inc idx) (. name -length)))))
(var? name) (.-sym name)
(keyword? name) (recur (.-fqn name))
:else (throw (new js/Error "no conversion to symbol"))))
([ns name]
(let [sym-str (if-not (nil? ns)
(str ns "/" name)
Expand Down Expand Up @@ -1182,11 +1190,6 @@ defaults to returning v."))
(-invoke [_ a b c d e f g h i j k l m n o p q r s t rest]
(apply (val) a b c d e f g h i j k l m n o p q r s t rest)))

(defn var?
"Returns true if v is of type cljs.core.Var"
[v]
(instance? cljs.core.Var v))

;;;;;;;;;;;;;;;;;;; fundamentals ;;;;;;;;;;;;;;;

(declare array-seq prim-seq IndexedSeq)
Expand Down
5 changes: 5 additions & 0 deletions src/test/cljs/cljs/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,11 @@
(is (= "#object[cljs.core.Atom {:val 1}]" (pr-str (atom 1))))
(is (= "#object[cljs.core.Volatile {:val 2}]" (pr-str (volatile! 2)))))

(deftest test-cljs-2944
(is (= (symbol :foo/bar) 'foo/bar))
(is (= (symbol (->Var nil 'bar/foo nil)) 'bar/foo))
(is (thrown? js/Error (symbol 1))))

(deftest test-cljs-2991
(let [o (js-obj)]
(is (object? o))
Expand Down

0 comments on commit b38ad7e

Please sign in to comment.