Skip to content

Commit

Permalink
feat: Add normalize-path
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Jan 26, 2024
1 parent e0a53f2 commit ddde8b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/elin/util/file.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns elin.util.file
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[elin.util.schema :as e.u.schema]
[malli.core :as m]))

Expand All @@ -19,3 +20,12 @@
(if (.exists file)
file
(recur (.getParentFile dir))))))))

(m/=> normalize-path [:=> [:cat string?] string?])
(defn normalize-path [path]
(let [path (str/replace-first path #"^file:" "")]
(if (str/starts-with? path "jar:")
(-> path
(str/replace-first #"^jar:file:" "zipfile://")
(str/replace #"!/" "::"))
path)))
9 changes: 9 additions & 0 deletions test/elin/util/file_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@

(t/testing "Not found"
(t/is (nil? (sut/find-file-in-parent-directories (str "non-existing" (random-uuid)))))))


(t/deftest normalize-path-test
(t/is (= "/foo/bar.txt"
(sut/normalize-path "/foo/bar.txt")))
(t/is (= "/foo/bar.txt"
(sut/normalize-path "file:/foo/bar.txt")))
(t/is (= "zipfile:///path/to/jarfile.jar::path/to/file.clj"
(sut/normalize-path "jar:file:/path/to/jarfile.jar!/path/to/file.clj"))))

0 comments on commit ddde8b1

Please sign in to comment.