Skip to content

Commit 9c2ec8b

Browse files
mfikesdnolen
authored and
dnolen
committed
CLJS-2300: Delegate clojure.string/capitalize to goog.string/capitalize
1 parent 9778b34 commit 9c2ec8b

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

benchmark/cljs/benchmark_runner.cljs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns cljs.benchmark-runner
22
(:refer-clojure :exclude [println])
33
(:require [cljs.reader :as reader]
4-
[clojure.core.reducers :as r]))
4+
[clojure.core.reducers :as r]
5+
[clojure.string :as string]))
56

67
(def println print)
78

@@ -408,6 +409,11 @@
408409
(simple-benchmark [] (str "1" "2") 1000000)
409410
(simple-benchmark [] (str "1" "2" "3") 1000000)
410411

412+
(println "\n")
413+
(println ";;; clojure.string")
414+
(simple-benchmark [s "a" f clojure.string/capitalize] (f s) 1000000)
415+
(simple-benchmark [s "aBcDeF" f clojure.string/capitalize] (f s) 1000000)
416+
411417
(println ";; printing of numbers and handling of ##Nan, ##Inf, ##-Inf")
412418
(simple-benchmark [x true] (pr-str x) 1000000)
413419
(simple-benchmark [x 10] (pr-str x) 1000000)

src/main/cljs/clojure/string.cljs

+1-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@
101101
"Converts first character of the string to upper-case, all other
102102
characters to lower-case."
103103
[s]
104-
(if (< (count s) 2)
105-
(upper-case s)
106-
(str (upper-case (subs s 0 1))
107-
(lower-case (subs s 1)))))
104+
(gstring/capitalize s))
108105

109106
;; The JavaScript split function takes a limit argument but the return
110107
;; value is not the same as the Java split function.

src/test/cljs/clojure/string_test.cljs

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
(ns clojure.string-test
1010
(:require [cljs.test :as test
1111
:refer-macros [deftest is testing]]
12+
[clojure.test.check :as tc]
13+
[clojure.test.check.clojure-test :refer-macros [defspec]]
14+
[clojure.test.check.generators :as gen]
15+
[clojure.test.check.properties :as prop :include-macros true]
1216
[clojure.string :as s]))
1317

1418
(deftest test-api
@@ -149,6 +153,22 @@
149153
(is (s/includes? sb "Applied"))
150154
(is (not (s/includes? sb "Living"))))))
151155

156+
(defspec test-cljs-2300
157+
;; The reference implementation is the implementation prior to the change.
158+
;; Since some JavaScript implementations fail to properly change case for
159+
;; some characters (for example, the upper case of "ß" is "SS"), we limit
160+
;; this test to strings comprising only printable ASCII characters.
161+
(let [ref-impl (fn [s]
162+
(if (< (count s) 2)
163+
(s/upper-case s)
164+
(str (s/upper-case (subs s 0 1))
165+
(s/lower-case (subs s 1)))))
166+
char-codes->string (fn [xs]
167+
(apply (.-fromCharCode js/String) xs))]
168+
(prop/for-all [s (gen/fmap char-codes->string
169+
(gen/not-empty (gen/vector (gen/choose 0x20 0x7E))))]
170+
(= (ref-impl s) (s/capitalize s)))))
171+
152172
(comment
153173

154174
(deftest char-sequence-handling

0 commit comments

Comments
 (0)