Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions path.carp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ As such, it is the inverse to [split](#split).")

(doc filename "gets the filename of the path `p` as a `(Maybe String)`.

For an empty path, the result is `Just` of an empty string, never `Nothing`.")
(defn filename [p] (Array.last &(split p)))
Returns `Nothing` when the path has no filename component: the empty path, a
bare root like `/`, or a path ending in a separator such as `foo/bar/`.
Otherwise returns `Just` of the last component.")
(defn filename [p]
(Maybe.filter (Array.last &(split p)) &(fn [f] (not (String.empty? f)))))
(doc basename "gets the basename of the path `p`.")
(defn basename [p]
(let [split (split p)
Expand Down
20 changes: 18 additions & 2 deletions test/path.carp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,25 @@
&(filename "path/to/file.txt")
"filename works")
(assert-equal test
&(Maybe.Just @"")
&(Maybe.Just @"c")
&(filename "a/b/c")
"filename works on nested paths")
(assert-equal test
&(Maybe.Just @"foo")
&(filename "foo")
"filename works on a single component")
(assert-equal test
&(Maybe.Nothing)
&(filename "")
"filename returns empty string for empty path")
"filename returns Nothing for an empty path")
(assert-equal test
&(Maybe.Nothing)
&(filename "/")
"filename returns Nothing for the root path")
(assert-equal test
&(Maybe.Nothing)
&(filename "foo/")
"filename returns Nothing for a directory path")
(assert-equal test
&(Maybe.Just (Pair.init @"file" @"txt"))
&(split-extension "file.txt")
Expand Down
Loading