Skip to content

Commit

Permalink
Add a without function to remove unwanted characters from a string.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmwaikar committed Oct 18, 2011
1 parent c01fa64 commit 84d9e01
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/codionics/utilities/str_utils.clj
@@ -1,4 +1,6 @@
(ns codionics.utilities.str-utils)
(ns codionics.utilities.str-utils
(:refer-clojure :exclude [replace])
(:use [clojure.string :only [replace]]))

(defn- betw [s start start-index end-index]
{:pre [(and (>= start-index 0) (< start-index (count s)))]}
Expand All @@ -13,4 +15,10 @@
end-index (.indexOf s end)]
(if (or (= end-index -1) (> end-index len))
(betw s start start-index len)
(betw s start start-index end-index))))
(betw s start start-index end-index))))

(defn without [s & args]
"Replaces each of the args with an empty string (i.e. \"\") and returns the resulting string."
(if (empty? args)
s
(recur (replace s (str (first args)) "") (rest args))))
9 changes: 7 additions & 2 deletions test/codionics/utilities/test/str_utils.clj
@@ -1,12 +1,17 @@
(ns codionics.utilities.test.str-utils
(:use [codionics.utilities.str-utils :only [between]]
(:use [codionics.utilities.str-utils :only [between without]]
[midje.sweet]))

;; Globals
(def *test-string* "and miles to go before I sleep")
(def *test-string-with-punctuations* "and miles\n to, go before, I sleep\n")

;; Tests

(fact (between *test-string* "miles" "I") => (just " to go before "))
(fact (between *test-string* "miles" "slept") => (just " to go before I sleep"))
(fact (between *test-string* "miley" "I") => (throws AssertionError (contains "Assert failed:")))
(fact (between *test-string* "miley" "I") => (throws AssertionError (contains "Assert failed:")))

(fact (without (between *test-string-with-punctuations* "miles" "slept") "\n") => (just " to, go before, I sleep"))
(fact (without (between *test-string-with-punctuations* "miles" "slept") "\n" ",") => (just " to go before I sleep"))
(fact (without (between *test-string-with-punctuations* "miles" "slept") "\n" " " ",") => (just "togobeforeIsleep"))

0 comments on commit 84d9e01

Please sign in to comment.