Skip to content

Commit 371d590

Browse files
tmarbleswannodette
authored andcommitted
CLJS-1441: Add clojure.string functions for portability to cljs as for clj
This is a direct port of the recent changes to Clojure from the ticket http://dev.clojure.org/jira/browse/CLJ-1449 with the exception that metadata and (non boolean) type hinting has been elided.
1 parent ce46b80 commit 371d590

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

src/main/cljs/clojure/string.cljs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,46 @@
213213
(.append buffer (str replacement))
214214
(.append buffer ch))
215215
(recur (inc index)))))))
216+
217+
(defn index-of
218+
"Return index of value (string or char) in s, optionally searching
219+
forward from from-index or nil if not found."
220+
([s value]
221+
(let [result (.indexOf s value)]
222+
(if (neg? result)
223+
nil
224+
result)))
225+
([s value from-index]
226+
(let [result (.indexOf s value from-index)]
227+
(if (neg? result)
228+
nil
229+
result))))
230+
231+
(defn last-index-of
232+
"Return last index of value (string or char) in s, optionally
233+
searching backward from from-index or nil if not found."
234+
([s value]
235+
(let [result (.lastIndexOf s value)]
236+
(if (neg? result)
237+
nil
238+
result)))
239+
([s value from-index]
240+
(let [result (.lastIndexOf s value from-index)]
241+
(if (neg? result)
242+
nil
243+
result))))
244+
245+
(defn ^boolean starts-with?
246+
"True if s starts with substr."
247+
[s substr]
248+
(gstring/startsWith s substr))
249+
250+
(defn ^boolean ends-with?
251+
"True if s ends with substr."
252+
[s substr]
253+
(gstring/endsWith s substr))
254+
255+
(defn ^boolean includes?
256+
"True if s includes substr."
257+
[s substr]
258+
(gstring/contains s substr))

src/test/cljs/clojure/string_test.cljs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(is (= "tab" (s/reverse "bat")))
1010
(is (= "c\uD834\uDD1Ea" (s/reverse "a\uD834\uDD1Ec"))) ;; U+1D11E MUSICAL SYMBOL G CLEF
1111
)
12-
12+
1313
(testing "Testing string replace"
1414
(is (= "faabar" (s/replace "foobar" \o \a)))
1515
(is (= "barbarbar" (s/replace "foobarfoo" "foo" "bar")))
@@ -87,7 +87,57 @@
8787
(is (= "foo" (s/trim-newline "foo\r\n")))
8888
(is (= "foo" (s/trim-newline "foo")))
8989
(is (= "foo\r " (s/trim-newline "foo\r ")))
90-
(is (= "" (s/trim-newline "")))))
90+
(is (= "" (s/trim-newline ""))))
91+
92+
(testing "Testing string trim-newline"
93+
(is (= "foo" (s/trim-newline "foo\n")))
94+
(is (= "foo" (s/trim-newline "foo\r\n")))
95+
(is (= "foo" (s/trim-newline "foo")))
96+
(is (= "foo\r " (s/trim-newline "foo\r ")))
97+
(is (= "" (s/trim-newline ""))))
98+
99+
(testing "Testing string index-of"
100+
(let [sb "tacos"]
101+
(is (= 2 (s/index-of sb "c")))
102+
(is (= 2 (s/index-of sb \c)))
103+
(is (= 1 (s/index-of sb "ac")))
104+
(is (= 3 (s/index-of sb "o" 2)))
105+
(is (= 3 (s/index-of sb \o 2)))
106+
(is (= 3 (s/index-of sb "o" -100)))
107+
(is (= nil (s/index-of sb "z")))
108+
(is (= nil (s/index-of sb \z)))
109+
(is (= nil (s/index-of sb "z" 2)))
110+
(is (= nil (s/index-of sb \z 2)))
111+
(is (= nil (s/index-of sb "z" 100))
112+
(is (= nil (s/index-of sb "z" -10))))))
113+
114+
(testing "Testing string last-index-of"
115+
(let [sb "banana"]
116+
(is (= 4 (s/last-index-of sb "n")))
117+
(is (= 4 (s/last-index-of sb \n)))
118+
(is (= 3 (s/last-index-of sb "an")))
119+
(is (= 4 (s/last-index-of sb "n" )))
120+
(is (= 4 (s/last-index-of sb "n" 5)))
121+
(is (= 4 (s/last-index-of sb \n 5)))
122+
(is (= 4 (s/last-index-of sb "n" 500)))
123+
(is (= nil (s/last-index-of sb "z")))
124+
(is (= nil (s/last-index-of sb "z" 1)))
125+
(is (= nil (s/last-index-of sb \z 1)))
126+
(is (= nil (s/last-index-of sb "z" 100)))
127+
(is (= nil (s/last-index-of sb "z" -10)))))
128+
129+
(testing "Testing string starts-with?"
130+
(is (s/starts-with? "clojure west" "clojure"))
131+
(is (not (s/starts-with? "conj" "clojure"))))
132+
133+
(testing "Testing string ends-with?"
134+
(is (s/ends-with? "Clojure West" "West"))
135+
(is (not (s/ends-with? "Conj" "West"))))
136+
137+
(testing "Testing string includes?"
138+
(let [sb "Clojure Applied Book"]
139+
(is (s/includes? sb "Applied"))
140+
(is (not (s/includes? sb "Living"))))))
91141

92142
(comment
93143

0 commit comments

Comments
 (0)