From 877a361da1c6904aa7f372aedf98b5fcf16e1e8d Mon Sep 17 00:00:00 2001 From: David Nolen Date: Wed, 24 Apr 2013 21:09:20 -0400 Subject: [PATCH] CLJS-496: better implementation of `char` 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. --- src/clj/cljs/core.clj | 6 ++++-- src/cljs/cljs/core.cljs | 12 ++++++++++-- test/cljs/cljs/core_test.cljs | 4 ++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/clj/cljs/core.clj b/src/clj/cljs/core.clj index 0a211f1d91..68c0751527 100644 --- a/src/clj/cljs/core.clj +++ b/src/clj/cljs/core.clj @@ -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 @@ -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)) @@ -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) diff --git a/src/cljs/cljs/core.cljs b/src/cljs/cljs/core.cljs index be7b8efa52..749735dc11 100644 --- a/src/cljs/cljs/core.cljs +++ b/src/cljs/cljs/core.cljs @@ -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))) @@ -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) diff --git a/test/cljs/cljs/core_test.cljs b/test/cljs/cljs/core_test.cljs index db7f60fa13..5c01cd689d 100644 --- a/test/cljs/cljs/core_test.cljs +++ b/test/cljs/cljs/core_test.cljs @@ -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 )