Skip to content

Commit

Permalink
feat: Add distinct-directory function
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Dec 28, 2023
1 parent d8b4c5f commit 2715bb5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/antq/util/file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@
const.project-file/maven :pom
const.project-file/shadow-cljs :shadow-cljs
::unknown))

(defn distinct-directory
[dirs]
(:result
(reduce
(fn [{:as accm :keys [fixme]} dir]
(let [path (if (str/starts-with? dir "~")
dir
(normalize-path (.getAbsolutePath (io/file dir))))]
(if (contains? fixme path)
accm
(-> accm
(update :fixme conj path)
(update :result conj dir)))))
{:fixme #{} :result []}
dirs)))
13 changes: 13 additions & 0 deletions test/antq/util/file_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[antq.util.env :as u.env]
[antq.util.file :as sut]
[clojure.java.io :as io]
[clojure.test :as t]))

(t/deftest normalize-path-test
Expand All @@ -26,3 +27,15 @@
:leiningen "/path/to/project.clj"
:shadow-cljs "/path/to/shadow-cljs.edn"
::sut/unknown "/path/to/invalid"))

(t/deftest distinct-directory-test
(let [absolute-path (.getAbsolutePath (io/file "."))
relative-path (sut/normalize-path absolute-path)]
(t/is (= ["a" "b" "c"]
(sut/distinct-directory ["a" "b" "c"])))
(t/is (= ["."]
(sut/distinct-directory ["." absolute-path relative-path])))
(t/is (= [absolute-path]
(sut/distinct-directory [absolute-path relative-path "."])))
(t/is (= ["a" absolute-path]
(sut/distinct-directory ["a" absolute-path relative-path])))))

0 comments on commit 2715bb5

Please sign in to comment.