Skip to content

Commit b38ad7e

Browse files
souenzzomfikes
authored andcommitted
CLJS-2958 - make symbol work on keywords and vars
1 parent e523cfa commit b38ad7e

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,16 +1098,24 @@ defaults to returning v."))
10981098
IPrintWithWriter
10991099
(-pr-writer [o writer _] (-write writer str)))
11001100

1101+
(defn var?
1102+
"Returns true if v is of type cljs.core.Var"
1103+
[v]
1104+
(instance? cljs.core.Var v))
1105+
11011106
(defn symbol
1102-
"Returns a Symbol with the given namespace and name."
1107+
"Returns a Symbol with the given namespace and name. Arity-1 works
1108+
on strings, keywords, and vars."
11031109
([name]
1104-
(if (symbol? name)
1105-
name
1106-
(let [idx (.indexOf name "/")]
1107-
(if (< idx 1)
1108-
(symbol nil name)
1109-
(symbol (.substring name 0 idx)
1110-
(.substring name (inc idx) (. name -length)))))))
1110+
(cond (symbol? name) name
1111+
(string? name) (let [idx (.indexOf name "/")]
1112+
(if (< idx 1)
1113+
(symbol nil name)
1114+
(symbol (.substring name 0 idx)
1115+
(.substring name (inc idx) (. name -length)))))
1116+
(var? name) (.-sym name)
1117+
(keyword? name) (recur (.-fqn name))
1118+
:else (throw (new js/Error "no conversion to symbol"))))
11111119
([ns name]
11121120
(let [sym-str (if-not (nil? ns)
11131121
(str ns "/" name)
@@ -1182,11 +1190,6 @@ defaults to returning v."))
11821190
(-invoke [_ a b c d e f g h i j k l m n o p q r s t rest]
11831191
(apply (val) a b c d e f g h i j k l m n o p q r s t rest)))
11841192

1185-
(defn var?
1186-
"Returns true if v is of type cljs.core.Var"
1187-
[v]
1188-
(instance? cljs.core.Var v))
1189-
11901193
;;;;;;;;;;;;;;;;;;; fundamentals ;;;;;;;;;;;;;;;
11911194

11921195
(declare array-seq prim-seq IndexedSeq)

src/test/cljs/cljs/core_test.cljs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,11 @@
17251725
(is (= "#object[cljs.core.Atom {:val 1}]" (pr-str (atom 1))))
17261726
(is (= "#object[cljs.core.Volatile {:val 2}]" (pr-str (volatile! 2)))))
17271727

1728+
(deftest test-cljs-2944
1729+
(is (= (symbol :foo/bar) 'foo/bar))
1730+
(is (= (symbol (->Var nil 'bar/foo nil)) 'bar/foo))
1731+
(is (thrown? js/Error (symbol 1))))
1732+
17281733
(deftest test-cljs-2991
17291734
(let [o (js-obj)]
17301735
(is (object? o))

0 commit comments

Comments
 (0)