Here is the original code block for ClojureScript's string "replace" function:
(defn replace
"Replaces all instance of match with replacement in s.
match/replacement can be:
string / string
pattern / (string or function of match)."
[s match replacement]
(cond
(string? match)
(.replace s (js/RegExp. (gstring/regExpEscape match) "g") replacement)
(instance? js/RegExp match)
(if (string? replacement)
(replace-all s match replacement)
(replace-all s match (replace-with replacement)))
:else (throw (str "Invalid match arg: " match))))
As you can see on this line:[s match replacement], this method accepts three arguments.
From my REPL:
user=> (replace ":c41120" ":" "")
ArityException Wrong number of args (3) passed to: core/replace clojure.lang.AFn.throwArity (AFn.java:429)
Am I the only one who thinks I have passed the correct number of arguments (3)? Any idea why this is failing?
Question, Part II: Getting Specific
In my components.cljs file, I have these 'requires':
(ns labrador.components
(:require [re-frame.core :as rf]
[reagent.core :refer [atom]]
[clojure.string :as s]
[labrador.helpers :as h]))
I've had success using "s/join" and "s/blank?" in this file. But when I try using "s/replace" like below (note that the "replace" call is on line 484):
(for [roll-count order-item-roll-counts]
(let [key (key roll-count)
val (val roll-count)
code (s/replace key ":" "")]
...I get the following error:
Uncaught TypeError: s.replace is not a function
at clojure$string$replace (string.cljs?rel=1489020198332:48)
at components.cljs?rel=1489505254528:484
...And when I explicitly call the replace function, like so:
code (clojure.string/replace key ":" "")]
...I still get the exact same error, as if I'm still calling "s/replace."
I'm new to Clojure/ClojureScript, so bare with my apparent ignorance.
Firstly, it looks like you're running in a Clojure REPL, not a ClojureScript one, secondly, you're calling clojure.core/replace, instead of clojure.string/replace.
I found the error. I was trying to do a replace on a key, not a string. Once I converted the key to a string before calling the replace function, by changing (s/replace key ":" "") with (s/replace (str key) ":" ""), all was well.
I was thrown way off-course by the ambiguous error message. Being told the function 'replace' isn't a function when it clearly is, rather than being told the function can't perform it's job because the data passed isn't a string, just cost me about three hours of dev time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With