Skip to content

Commit

Permalink
CLJS-496: better implementation of char
Browse files Browse the repository at this point in the history
Stop relying on `goog/isNumber?`, implement `number?` as an inlining
macro in core.clj, use it in core.cljs. Change `char` implementation
so that it uses `String.fromCharCode` if given a number. It simply
returns strings of length 1. On any other type of input it throws an
exception.
  • Loading branch information
swannodette committed Apr 25, 2013
1 parent 2f3c4b7 commit 877a361
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/clj/cljs/core.clj
Expand Up @@ -15,7 +15,7 @@
memfn ns or proxy proxy-super pvalues refer-clojure reify sync time
when when-first when-let when-not while with-bindings with-in-str
with-loading-context with-local-vars with-open with-out-str with-precision with-redefs
satisfies? identical? true? false? nil? str get
satisfies? identical? true? false? number? nil? str get

aget aset
+ - * / < <= > >= == zero? pos? neg? inc dec max min mod
Expand Down Expand Up @@ -222,6 +222,9 @@
(defmacro identical? [a b]
(bool-expr (list 'js* "(~{} === ~{})" a b)))

(defmacro number? [x]
(bool-expr (list 'js* "typeof ~{} === 'number'" x)))

(defmacro aget
([a i]
(list 'js* "(~{}[~{}])" a i))
Expand All @@ -239,7 +242,6 @@
([x y & more] `(+ (+ ~x ~y) ~@more)))

(defmacro byte [x] x)
(defmacro char [x] x)
(defmacro short [x] x)
(defmacro float [x] x)
(defmacro double [x] x)
Expand Down
12 changes: 10 additions & 2 deletions src/cljs/cljs/core.cljs
Expand Up @@ -1105,7 +1105,7 @@ reduces them without incurring seq initialization"
(instance? Symbol x))

(defn ^boolean number? [n]
(goog/isNumber n))
(cljs.core/number? n))

(defn ^boolean fn? [f]
(or ^boolean (goog/isFunction f) (satisfies? Fn f)))
Expand Down Expand Up @@ -1379,7 +1379,15 @@ reduces them without incurring seq initialization"
(reduce min (cljs.core/min x y) more)))

(defn byte [x] x)
(defn char [x] x)

(defn char
"Coerce to char"
[x]
(cond
(number? x) (.fromCharCode js/String x)
(and (string? x) (== (.-length x) 1)) x
:else (throw (js/Error. "Argument to char must be a character or number"))))

(defn short [x] x)
(defn float [x] x)
(defn double [x] x)
Expand Down
4 changes: 4 additions & 0 deletions test/cljs/cljs/core_test.cljs
Expand Up @@ -1857,5 +1857,9 @@
(def exists?-test-val 'foo)
(assert (exists? exists?-test-val))

;; CLJS-496
(assert (= (char 65) \A))
(assert (= (char \A) \A))

:ok
)

0 comments on commit 877a361

Please sign in to comment.