Permalink
Browse files

Migrate a bunch of string functions from Crawlista

  • Loading branch information...
1 parent 35efb76 commit 204f3dbdf89ec950cd405089188358f347fa7000 @michaelklishin michaelklishin committed Jun 11, 2012
Showing with 70 additions and 1 deletion.
  1. +6 −1 ChangeLog.md
  2. +33 −0 src/clojure/clojurewerkz/support/string.clj
  3. +31 −0 test/clojurewerkz/support/string_test.clj
View
7 ChangeLog.md
@@ -1,6 +1,11 @@
## Changes between ClojureWerkz Support 0.4.0 and 0.5.0
-No changes yet.
+### New clojurewerkz.support.string functions
+
+Several new functions were extracted from [Crawlista](https://github.com/michaelklishin/crawlista):
+
+`clojurewerkz.support.string/maybe-prepend`, `clojurewerkz.support.string/maybe-append`, `clojurewerkz.support.string/maybe-chopl`,
+`clojurewerkz.support.string/maybe-chopr`, `clojurewerkz.support.string/hex-to-int`.
View
33 src/clojure/clojurewerkz/support/string.clj
@@ -40,3 +40,36 @@
(let [s2 (.replaceAll s camel-case-splitter " ")
xs (s/split s2 #" ")]
(s/join "_" (map #(.toLowerCase ^String %) xs))))
+
+
+(defn maybe-prepend
+ [^String s ^String prefix]
+ (.toLowerCase (if (.startsWith (.toLowerCase s) (.toLowerCase prefix))
+ s
+ (str prefix s))))
+
+(defn maybe-append
+ [^String s ^String suffix]
+ (.toLowerCase (if (.endsWith (.toLowerCase s) (.toLowerCase suffix))
+ s
+ (str s suffix))))
+
+(defn maybe-chopl
+ [^String s ^String prefix]
+ (let [ls (.toLowerCase s)]
+ (if (.startsWith ls prefix)
+ (.replaceAll ls (str "^" prefix) "")
+ s)))
+
+(defn maybe-chopr
+ [^String s ^String suffix]
+ (let [ls (.toLowerCase s)]
+ (if (.endsWith ls suffix)
+ (.replaceAll ls (str suffix "$") "")
+ s)))
+
+(defn hex-to-int
+ [^String s]
+ (Long/parseLong (if (.startsWith s "0x")
+ (subs s 2)
+ s) 16))
View
31 test/clojurewerkz/support/string_test.clj
@@ -33,3 +33,34 @@
"PDFLoader" "pdf_loader"
"lowercase" "lowercase"
"SimpleXMLParser" "simple_xml_parser"))
+
+
+(deftest test-maybe-prepend
+ (is (= "www.apple.com" (s/maybe-prepend "apple.com" "www.")))
+ (is (= "www.apple.com" (s/maybe-prepend "APPLE.com" "www.")))
+ (is (= "www.apple.com" (s/maybe-prepend "www.apple.com" "www."))))
+
+(deftest test-maybe-append
+ (is (= "apple.com" (s/maybe-append "apple.com" ".com")))
+ (is (= "apple.com" (s/maybe-append "APPLE.com" ".com")))
+ (is (= "google.com" (s/maybe-append "google" ".com"))))
+
+(deftest test-maybe-chopl
+ (is (= (s/maybe-chopl "www.google.com" "www.") "google.com"))
+ (is (= (s/maybe-chopl "google.com" "goo") "gle.com"))
+ (is (= (s/maybe-chopl "Google.COM" "google.") "com"))
+ (is (= (s/maybe-chopl "www.www2.megacorp.net" "www.") "www2.megacorp.net")))
+
+(deftest test-maybe-chopr
+ (is (= (s/maybe-chopr "http://www.google.com/" "/") "http://www.google.com"))
+ (is (= (s/maybe-chopr "google.com/" ".com/") "google"))
+ (is (= (s/maybe-chopr "Google.COM/" "/") "google.com")))
+
+(deftest test-hexadecimal-to-int
+ (are [s i] (is (= (s/hex-to-int s) i))
+ "0xFF00FF" 16711935
+ "FF00FF" 16711935
+ "0xFF0000" 16711680
+ "FF0000" 16711680
+ "0x001100" 4352
+ "001100" 4352))

0 comments on commit 204f3db

Please sign in to comment.