From 2b4757367470d8e36bc00901dac567e375796ed4 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 13 Nov 2018 00:24:09 -0500 Subject: update viz 2 to use the newest version, which has some performance improvements and is easier to read the code for. also update the description --- assets/viz/2/clojure/string.cljs | 47 +++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'assets/viz/2/clojure/string.cljs') diff --git a/assets/viz/2/clojure/string.cljs b/assets/viz/2/clojure/string.cljs index 4cf5ede..26f63f2 100644 --- a/assets/viz/2/clojure/string.cljs +++ b/assets/viz/2/clojure/string.cljs @@ -26,7 +26,12 @@ (defn- replace-all [s re replacement] - (.replace s (js/RegExp. (.-source re) "g") replacement)) + (let [r (js/RegExp. (.-source re) + (cond-> "g" + (.-ignoreCase re) (str "i") + (.-multiline re) (str "m") + (.-unicode re) (str "u")))] + (.replace s r replacement))) (defn- replace-with [f] @@ -38,10 +43,24 @@ (defn replace "Replaces all instance of match with replacement in s. + match/replacement can be: string / string - pattern / (string or function of match)." + pattern / (string or function of match). + + See also replace-first. + + The replacement is literal (i.e. none of its characters are treated + specially) for all cases above except pattern / string. + + For pattern / string, $1, $2, etc. in the replacement string are + substituted with the string that matched the corresponding + parenthesized group in the pattern. + + Example: + (clojure.string/replace \"Almost Pig Latin\" #\"\\b(\\w)(\\w+)\\b\" \"$2$1ay\") + -> \"lmostAay igPay atinLay\"" [s match replacement] (cond (string? match) @@ -56,10 +75,25 @@ (defn replace-first "Replaces the first instance of match with replacement in s. + match/replacement can be: string / string - pattern / (string or function of match)." + pattern / (string or function of match). + + See also replace. + + The replacement is literal (i.e. none of its characters are treated + specially) for all cases above except pattern / string. + + For pattern / string, $1, $2, etc. in the replacement string are + substituted with the string that matched the corresponding + parenthesized group in the pattern. + + Example: + (clojure.string/replace-first \"swap first two words\" + #\"(\\w+)(\\s+)(\\w+)\" \"$3$2$1\") + -> \"first swap two words\"" [s match replacement] (.replace s match replacement)) @@ -96,10 +130,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. @@ -158,7 +189,7 @@ (conj parts s)))))))))) (defn split-lines - "Splits s on \n or \r\n." + "Splits s on \\n or \\r\\n." [s] (split s #"\n|\r\n")) -- cgit v1.2.3