From b56f43b1c13b96a0ceb9949a3112faa102261d77 Mon Sep 17 00:00:00 2001 From: Roman Gonzalez Date: Thu, 15 Mar 2012 19:15:33 -0700 Subject: [PATCH 1/2] * src/clj/cljs/core.clj: CLJS-162: fix broken str* implemenation --- src/cljs/cljs/core.cljs | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-) diff --git a/src/cljs/cljs/core.cljs b/src/cljs/cljs/core.cljs index 47d35a7..bedffb8 100644 --- a/src/cljs/cljs/core.cljs +++ b/src/cljs/cljs/core.cljs @@ -1034,6 +1034,8 @@ reduces them without incurring seq initialization" "Internal - do not use!" ([] "") ([x] (cond + (symbol? x) (. x (substring 2 (.-length x))) + (keyword? x) (str* ":" (. x (substring 2 (.-length x)))) (nil? x) "" :else (. x (toString)))) ([x & ys] -- 1.7.2.2 From 68853479527b1b5439c6fa90189535375f4ec270 Mon Sep 17 00:00:00 2001 From: Roman Gonzalez Date: Sat, 17 Mar 2012 05:48:51 +0100 Subject: [PATCH 2/2] * src/clj/cljs/core.clj: CLJS-162: fix broken str implemenation * Add private fn `gen-multiple-arity-str-fn` that generates functions that support multiple arity for both `str*` and `str` * Add basic tests for `str` --- src/cljs/cljs/core.cljs | 25 ++++++++++++++----------- test/cljs/cljs/core_test.cljs | 7 +++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/cljs/cljs/core.cljs b/src/cljs/cljs/core.cljs index bedffb8..2fbd59a 100644 --- a/src/cljs/cljs/core.cljs +++ b/src/cljs/cljs/core.cljs @@ -1030,22 +1030,26 @@ reduces them without incurring seq initialization" ;;;;;;;;;;;;;;;;;;;;;;;;;; basics ;;;;;;;;;;;;;;;;;; +(declare apply) + +(defn- gen-multiple-arity-str-fn + "Creates a multiple arity `str` like function using the goog/StringBuffer." + [single-arity-str-fn] + (fn str-fn [x & ys] + ((fn [sb more] + (if more + (recur (. sb (append (single-arity-str-fn (first more)))) (next more)) + (single-arity-str-fn sb))) + (gstring/StringBuffer. (single-arity-str-fn x)) ys))) + (defn- str* "Internal - do not use!" ([] "") ([x] (cond - (symbol? x) (. x (substring 2 (.-length x))) - (keyword? x) (str* ":" (. x (substring 2 (.-length x)))) (nil? x) "" :else (. x (toString)))) ([x & ys] - ((fn [sb more] - (if more - (recur (. sb (append (str* (first more)))) (next more)) - (str* sb))) - (gstring/StringBuffer. (str* x)) ys))) - -(declare apply) + (apply (gen-multiple-arity-str-fn str*) x ys))) (defn str "With no args, returns the empty string. With one arg x, returns @@ -1058,7 +1062,7 @@ reduces them without incurring seq initialization" (nil? x) "" :else (. x (toString)))) ([x & ys] - (apply str* x ys))) + (apply (gen-multiple-arity-str-fn str) x ys))) (defn subs "Returns the substring of s beginning at start inclusive, and ending @@ -1082,7 +1086,6 @@ reduces them without incurring seq initialization" ([ns name] (keyword (str* ns "/" name)))) - (defn- equiv-sequential "Assumes x is sequential. Returns true if x equals y, otherwise returns false." diff --git a/test/cljs/cljs/core_test.cljs b/test/cljs/cljs/core_test.cljs index 8e5fc69..85253a8 100644 --- a/test/cljs/cljs/core_test.cljs +++ b/test/cljs/cljs/core_test.cljs @@ -160,6 +160,12 @@ (assert (= "baz" (name :foo/bar/baz))) ;(assert (= "foo/bar" (namespace :foo/bar/baz))) + ; str + (assert (= ":hello" (str :hello))) + (assert (= "hello" (str 'hello))) + (assert (= "hello:world" (str "hello" :world))) + (assert (= ":holamundo" (str :hola 'mundo))) + (assert (= {:a :b} (get {[1 2 3] {:a :b}, 4 5} [1 2 3]))) (assert (= :a (nth [:a :b :c :d] 0))) (assert (= :a (nth [:a :b :c :d] 0.1)) ) @@ -530,6 +536,7 @@ (assert (= {1 2} (let [[a b] [1 2]] {a b}))) (assert (= [2 1] (let [[a b] (seq [1 2])] [b a]))) + ;; update-in (assert (= {:foo {:bar {:baz 1}}} (update-in {:foo {:bar {:baz 0}}} [:foo :bar :baz] inc))) -- 1.7.2.2