Skip to content

Commit

Permalink
[#29] Add strip-ext and change split-ext and strip-ext to return full…
Browse files Browse the repository at this point in the history
… paths
  • Loading branch information
corasaurus-hex authored Jul 22, 2021
1 parent 77215d0 commit 5339280
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
25 changes: 22 additions & 3 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -670,14 +670,33 @@
(.endsWith (as-path this) (as-path other)))

(defn split-ext
"Splits a path into a vec of [name ext]. Works with strings, files, or paths."
"Splits a path into a vec of [path-without-ext ext]. Works with strings, files, or paths."
[path]
(let [name (file-name path)
i (str/last-index-of name ".")
ext (when (and i (pos? i)) (subs name (+ 1 i)))]
(if ext
[(subs name 0 i) ext]
[name nil])))
(let [new-name (subs name 0 i)
new-path (-> (parent path) (babashka.fs/path new-name) str)]
[new-path ext])
[path nil])))

(defn strip-ext
"Returns the path with the extension removed. If provided, a specific extension will be removed."
([path]
(-> path split-ext first))
([path {:keys [ext]}]
(let [name (file-name path)
ext (str "." ext)
ext-index (str/last-index-of name ext)
has-ext? (and ext-index
(pos? ext-index)
(= ext-index (- (count name) (count ext))))]
(if has-ext?
(-> (parent path)
(babashka.fs/path (subs name 0 ext-index))
str)
path))))

(defn extension
"Returns the extension of a file"
Expand Down
21 changes: 19 additions & 2 deletions test/babashka/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
(str/replace p "\\" "/")
(str p)))

(defn as-os-path [p]
(if windows?
(str/replace p "/" "\\")
(str/replace p "\\" "/")))

(defn temp-dir []
(-> (fs/create-temp-dir)
(fs/delete-on-exit)))
Expand Down Expand Up @@ -303,8 +308,8 @@
(deftest split-ext-test
(testing "strings"
(is (= ["name" "clj"] (fs/split-ext "name.clj")))
(is (= ["file" "ext"] (fs/split-ext "/path/to/file.ext")))
(is (= ["hi.tar" "gz"] (fs/split-ext "some/path/hi.tar.gz")))
(is (= [(as-os-path "/path/to/file") "ext"] (fs/split-ext "/path/to/file.ext")))
(is (= [(as-os-path "some/path/hi.tar") "gz"] (fs/split-ext "some/path/hi.tar.gz")))
(is (= [".dotfile" nil] (fs/split-ext ".dotfile")))
(is (= ["name" nil] (fs/split-ext "name"))))

Expand All @@ -318,6 +323,18 @@
(is (= nil (fs/extension ".dotfile")))
(is (= nil (fs/extension "bin/something"))))

(deftest strip-ext-test
(is (= "file-name" (fs/strip-ext "file-name.clj")))
(is (= "file-name.html" (fs/strip-ext "file-name.html.template")))
(is (= "file-name" (fs/strip-ext "file-name.html.template" {:ext "html.template"})))
(is (= "file-name.html.template" (fs/strip-ext "file-name.html.template" {:ext "html"})))
(is (= (as-os-path "/path/to/file-name.html") (fs/strip-ext "/path/to/file-name.html.template")))
(is (= (as-os-path "path/to/file-name") (fs/strip-ext "path/to/file-name.html.template" {:ext "html.template"})))
(is (= "/path/to/file-name.html.template" (fs/strip-ext "/path/to/file-name.html.template" {:ext "html"})))
(is (= ".dotfile" (fs/strip-ext ".dotfile")))
(is (= ".dotfile" (fs/strip-ext ".dotfile" {:ext "dotfile"})))
(is (= "bin/something" (fs/strip-ext "bin/something"))))

(deftest modified-since-test
(let [td0 (fs/create-temp-dir)
anchor (fs/file td0 "f0")
Expand Down

0 comments on commit 5339280

Please sign in to comment.