diff --git a/path.carp b/path.carp index ff57372..cfd936d 100644 --- a/path.carp +++ b/path.carp @@ -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) diff --git a/test/path.carp b/test/path.carp index 255d680..adbc29c 100644 --- a/test/path.carp +++ b/test/path.carp @@ -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")