Skip to content

Commit 4662caa

Browse files
committed
Added method to get a file extension
1 parent dcaecbb commit 4662caa

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject org.ajoberstar/ike.cljj "0.3.0"
1+
(defproject org.ajoberstar/ike.cljj "0.4.0"
22
:description "Clojure to Java interop APIs"
33
:url "https://github.com/ajoberstar/ike.cljj"
44
:license {:name "Eclipse Public License"

src/ike/cljj/file.clj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
(:refer-clojure :exclude [list])
55
(:require [ike.cljj.stream :as stream]
66
[ike.cljj.function :refer [defsam]]
7-
[clojure.java.io :as io])
7+
[clojure.java.io :as io]
8+
[clojure.string :as str])
89
(:import (java.nio.file Path Paths Files CopyOption LinkOption OpenOption StandardOpenOption FileVisitOption SimpleFileVisitor FileVisitResult)
910
(java.nio.file.attribute FileAttribute)
1011
(java.nio.charset Charset StandardCharsets)
@@ -64,6 +65,14 @@
6465
(let [more-array (into-array String more)]
6566
(Paths/get x more-array)))
6667

68+
(defn extension
69+
"Gets the extension of the path, if any. Nil returned if there is no extension."
70+
[path]
71+
(let [name (-> path .getFileName str)
72+
begin (str/last-index-of name ".")]
73+
(when (and begin (< 0 begin) (< begin (dec (count name))))
74+
(subs name (inc begin)))))
75+
6776
(defn exists?
6877
"Tests whether the path exists."
6978
[path]

test/ike/cljj/file_test.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
(deftest path-accepts-multiple-args
3939
(is (instance? Path (file/path "/etc" "fstab"))))
4040

41+
(deftest extension-test
42+
(is (= "flac" (file/extension (file/path "/home" "person" "music.flac"))))
43+
(is (= "conf" (file/extension (file/path ".music.conf"))))
44+
(is (nil? (file/extension (file/path "/etc" "temp."))))
45+
(is (nil? (file/extension (file/path "/var" "log" "app" "things")))))
46+
4147
(deftest make-dir-test
4248
(let [path (.resolve (file/temp-dir "make-dir") "the-dir")]
4349
(is (not (file/exists? path)))

0 commit comments

Comments
 (0)