Skip to content

Commit

Permalink
Added method to get a file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoberstar committed Jul 23, 2017
1 parent dcaecbb commit 4662caa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject org.ajoberstar/ike.cljj "0.3.0"
(defproject org.ajoberstar/ike.cljj "0.4.0"
:description "Clojure to Java interop APIs"
:url "https://github.com/ajoberstar/ike.cljj"
:license {:name "Eclipse Public License"
Expand Down
11 changes: 10 additions & 1 deletion src/ike/cljj/file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
(:refer-clojure :exclude [list])
(:require [ike.cljj.stream :as stream]
[ike.cljj.function :refer [defsam]]
[clojure.java.io :as io])
[clojure.java.io :as io]
[clojure.string :as str])
(:import (java.nio.file Path Paths Files CopyOption LinkOption OpenOption StandardOpenOption FileVisitOption SimpleFileVisitor FileVisitResult)
(java.nio.file.attribute FileAttribute)
(java.nio.charset Charset StandardCharsets)
Expand Down Expand Up @@ -64,6 +65,14 @@
(let [more-array (into-array String more)]
(Paths/get x more-array)))

(defn extension
"Gets the extension of the path, if any. Nil returned if there is no extension."
[path]
(let [name (-> path .getFileName str)
begin (str/last-index-of name ".")]
(when (and begin (< 0 begin) (< begin (dec (count name))))
(subs name (inc begin)))))

(defn exists?
"Tests whether the path exists."
[path]
Expand Down
6 changes: 6 additions & 0 deletions test/ike/cljj/file_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
(deftest path-accepts-multiple-args
(is (instance? Path (file/path "/etc" "fstab"))))

(deftest extension-test
(is (= "flac" (file/extension (file/path "/home" "person" "music.flac"))))
(is (= "conf" (file/extension (file/path ".music.conf"))))
(is (nil? (file/extension (file/path "/etc" "temp."))))
(is (nil? (file/extension (file/path "/var" "log" "app" "things")))))

(deftest make-dir-test
(let [path (.resolve (file/temp-dir "make-dir") "the-dir")]
(is (not (file/exists? path)))
Expand Down

0 comments on commit 4662caa

Please sign in to comment.