Commits

Anonymous committed bcbf607

add filename-splitting functions

Comments (0)

Files changed (2)

src/main/clj/org/bituf/clj_miscutil.clj

   (last (split-filepath s)))
 
 
+(defn split-filename
+  "Given a partial or complete file path, split into filename and extension and
+  return as vector."
+  [s]
+  (let [f   (pick-filename s)
+        sfe (sr/split f #"\.")
+        sfc (count sfe)
+        sf1 (first sfe)
+        sf2 (second sfe)]
+    (cond
+      (= 1 sfc)                    (conj sfe "")
+      (and (= 2 sfc) (empty? sf1)) [(str "." sf2) ""]
+      :else                        [(sr/join "." (drop-last sfe)) (last sfe)])))
+
+
+(defn ^String pick-filename-name
+  "Given a filepath, return the filename (without extension) portion from it."
+  [s]
+  (first (split-filename s)))
+
+
+(defn ^String pick-filename-ext
+  "Given a filepath, return the file extension portion from it."
+  [s]
+  (last (split-filename s)))
+
+
 (defn ^String as-vstr
   "Convert to verbose string - useful for diagnostics and error messages. Like
   as-string, but distinguishes nil as \"<nil>\". 

src/test/clj/org/bituf/test_clj_miscutil.clj

       (is (= "C:/path/to/file.txt" (java-filepath "C:\\path\\to\\file.txt"))))
     (testing "split-filepath"
       (is (= ["C:/path/to" "file.txt"] (split-filepath "C:\\path\\to\\file.txt")))
-      (is (= ["" "file.txt"] (split-filepath "file.txt"))))
+      (is (= ["/path/to" "file.txt"]   (split-filepath "/path/to/file.txt")))
+      (is (= ["" "file.txt"]           (split-filepath "file.txt"))))
     (testing "pick-filedir"
       (is (= "C:/path/to" (pick-filedir "C:\\path\\to\\file.txt")))
       (is (= ""           (pick-filedir "file.txt"))))
     (testing "pick-filename"
-      (is (= "file.txt" (pick-filename "C:\\path\\to\\file.txt")))
-      (is (= "file.txt" (pick-filename "file.txt"))))
+      (is (= "file.txt"   (pick-filename "C:\\path\\to\\file.txt")))
+      (is (= "file.txt"   (pick-filename "file.txt"))))
+    (testing "split-filename"
+      (is (= ["file" "txt"]      (split-filename "C:\\path\\to\\file.txt")))
+      (is (= ["file" "txt"]      (split-filename "/path/to/file.txt")))
+      (is (= ["file" "txt"]      (split-filename "file.txt")))
+      (is (= ["file.exe"  "txt"] (split-filename "file.exe.txt")))
+      (is (= [".file.exe" "txt"] (split-filename ".file.exe.txt")))
+      (is (= [".txt" ""]         (split-filename ".txt")))
+      (is (= ["file" ""]         (split-filename "file"))))
+    (testing "pick-filename-name"
+      (is (= "file" (pick-filename-name "C:\\path\\to\\file.txt")))
+      (is (= "file" (pick-filename-name "file.txt")))
+      (is (= ".txt" (pick-filename-name ".txt"))))
+    (testing "pick-filename-ext"
+      (is (= "txt"  (pick-filename-ext "C:\\path\\to\\file.txt")))
+      (is (= "txt"  (pick-filename-ext "file.txt")))
+      (is (= "txt"  (pick-filename-ext "file.exe.txt")))
+      (is (= ""     (pick-filename-ext ".txt")))
+      (is (= ""     (pick-filename-ext "file"))))
     (testing "as-vstr"
       (testall as-vstr
         {10 "10", nil "<nil>", "hello" "hello", :kw "kw"}))