Skip to content

Commit

Permalink
CLJS-2300: Delegate clojure.string/capitalize to goog.string/capitalize
Browse files Browse the repository at this point in the history
  • Loading branch information
mfikes authored and dnolen committed Oct 1, 2017
1 parent 9778b34 commit 9c2ec8b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
8 changes: 7 additions & 1 deletion benchmark/cljs/benchmark_runner.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns cljs.benchmark-runner
(:refer-clojure :exclude [println])
(:require [cljs.reader :as reader]
[clojure.core.reducers :as r]))
[clojure.core.reducers :as r]
[clojure.string :as string]))

(def println print)

Expand Down Expand Up @@ -408,6 +409,11 @@
(simple-benchmark [] (str "1" "2") 1000000)
(simple-benchmark [] (str "1" "2" "3") 1000000)

(println "\n")
(println ";;; clojure.string")
(simple-benchmark [s "a" f clojure.string/capitalize] (f s) 1000000)
(simple-benchmark [s "aBcDeF" f clojure.string/capitalize] (f s) 1000000)

(println ";; printing of numbers and handling of ##Nan, ##Inf, ##-Inf")
(simple-benchmark [x true] (pr-str x) 1000000)
(simple-benchmark [x 10] (pr-str x) 1000000)
Expand Down
5 changes: 1 addition & 4 deletions src/main/cljs/clojure/string.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@
"Converts first character of the string to upper-case, all other
characters to lower-case."
[s]
(if (< (count s) 2)
(upper-case s)
(str (upper-case (subs s 0 1))
(lower-case (subs s 1)))))
(gstring/capitalize s))

;; The JavaScript split function takes a limit argument but the return
;; value is not the same as the Java split function.
Expand Down
20 changes: 20 additions & 0 deletions src/test/cljs/clojure/string_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
(ns clojure.string-test
(:require [cljs.test :as test
:refer-macros [deftest is testing]]
[clojure.test.check :as tc]
[clojure.test.check.clojure-test :refer-macros [defspec]]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop :include-macros true]
[clojure.string :as s]))

(deftest test-api
Expand Down Expand Up @@ -149,6 +153,22 @@
(is (s/includes? sb "Applied"))
(is (not (s/includes? sb "Living"))))))

(defspec test-cljs-2300
;; The reference implementation is the implementation prior to the change.
;; Since some JavaScript implementations fail to properly change case for
;; some characters (for example, the upper case of "ß" is "SS"), we limit
;; this test to strings comprising only printable ASCII characters.
(let [ref-impl (fn [s]
(if (< (count s) 2)
(s/upper-case s)
(str (s/upper-case (subs s 0 1))
(s/lower-case (subs s 1)))))
char-codes->string (fn [xs]
(apply (.-fromCharCode js/String) xs))]
(prop/for-all [s (gen/fmap char-codes->string
(gen/not-empty (gen/vector (gen/choose 0x20 0x7E))))]
(= (ref-impl s) (s/capitalize s)))))

(comment

(deftest char-sequence-handling
Expand Down

0 comments on commit 9c2ec8b

Please sign in to comment.