Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

The ClojureScript implementation of the {{clojure.string/escape}} function assumes that the {{cmap}} parameter will always be a map. This makes it different from (and specifically less general than) the Clojure implementation of the same function, which permits {{cmap}} to be anything callable.

Here's the relevant lines of the {{clojure.string/escape}} implementations in (link: https://github.com/clojure/clojure/blob/master/src/clj/clojure/string.clj#L313 text: Clojure) and (link: https://github.com/clojure/clojurescript/blob/master/src/main/cljs/clojure/string.cljs#L216 text: ClojureScript). The ClojureScript implementation calls {{get}} on {{cmap}}, while the Clojure implementation invokes {{cmap}} directly.

Here's an example that works on Clojure, but doesn't work on ClojureScript, because it passes a function to {{clojure.string/escape}} instead of a map:

`
(defn regex-escape
"Escapes regex special chars in the string s."
[s]
(let [special? #{\- \[ \] \{ \} \( \) \* \+ \? \. \\ \^ \$ \|}]

(clojure.string/escape s #(when (special? %) (str \\ %)))))

`

Ideally, this discrepancy would be fixed by changing the ClojureScript implementation of {{clojure.string/escape}} to follow the Clojure one. This would also match the behavior described in the function's docstring, which is the same on both platforms.

2 Answers

0 votes
by

Comment made by: mfikes

0001-CLJS-2268-Make-clojure.string-escape-constent-with-C.patch added to Patch Tender (i)

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJS-2268 (reported by alex+import)
...