Skip to content

Commit

Permalink
Fix #81: do not touch directories in split-ext
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Dec 7, 2022
1 parent 5098aa7 commit deadb9d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ For a list of breaking changes, check [here](#breaking-changes).

Babashka [fs](https://github.com/babashka/fs): file system utility library for Clojure

## Unreleased

- [#81](https://github.com/babashka/fs/issues/81): do not process directories in `strip-ext` and `split-ext`

## v0.2.12 (2022-11-16)

- [#73](https://github.com/babashka/fs/issues/73): add [`write-bytes`](https://github.com/babashka/fs/blob/master/API.md#babashka.fs/write-bytes) and [`write-lines`](https://github.com/babashka/fs/blob/master/API.md#babashka.fs/write-lines)
Expand Down
52 changes: 27 additions & 25 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@
;;;; End predicates

(defn components
"Returns a seq of all components of f."
"Returns a seq of all components of f as paths, i.e. split on the file
separator."
[f]
(seq (as-path f)))

Expand Down Expand Up @@ -737,34 +738,35 @@
(mapcat #(list-dir % glob-or-accept) dirs))

(defn split-ext
"Splits a path into a vec of [path-without-ext ext]. Works with strings, files, or paths."
[path]
(let [name (str path)
i (str/last-index-of name ".")
ext (when (and i (pos? i)) (subs name (+ 1 i)))]
(if ext
(let [new-name (subs name 0 i)]
[new-name ext])
[path nil])))
"Splits path on extension If provided, a specific extension `ext`, the
extension (without dot), will be used for splitting. Directories
are not processed."
([path] (split-ext path nil))
([path {:keys [ext]}]
(let [path-str (str path)]
(if (directory? path)
[path-str nil]
(let [ext (if ext
(str "." ext)
(when-let [last-dot (str/last-index-of path-str ".")]
(subs path-str last-dot)))]
(if (and ext
(str/ends-with? path-str ext)
(not= path-str ext))
(let [loc (str/last-index-of path-str ext)]
[(subs path-str 0 loc)
(subs path-str (inc loc))])
[path-str nil]))))))

(defn strip-ext
"Returns the path with the extension removed. If provided, a specific extension will be removed."
"Strips extension via `split-ext`."
([path]
(-> path split-ext first))
([path {:keys [ext]}]
(let [name (str 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?
(-> (subs name 0 ext-index)
str)
(str path)))))
(strip-ext path nil))
([path {:keys [ext] :as opts}]
(first (split-ext path opts))))

(defn extension
"Returns the extension of a file"
"Returns the extension of a file via `split-ext`."
[path]
(-> path split-ext last))

Expand Down Expand Up @@ -1023,7 +1025,7 @@
(defn windows?
"Returns true if OS is Windows."
[]
(str/starts-with? (System/getProperty "os.name") "Windows"))
win?)

(defn cwd
"Returns current working directory as path"
Expand Down
3 changes: 2 additions & 1 deletion test/babashka/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@
(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"))))
(is (= "bin/something" (fs/strip-ext "bin/something")))
(is (= "test-resources/dir-with.ext" (fs/strip-ext "test-resources/dir-with.ext"))))

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

0 comments on commit deadb9d

Please sign in to comment.