Path
is a simple file path library for Carp.
Installation
(load "git@git.veitheller.de:carpentry/path.git@0.0.4")
Usage
The Path
module mostly operates on String
arguments. It allows you to
split, join, and merge paths and extensions in a lot of different ways. It also
has some functions to work with the PATH
environment variable.
It assumes either Windows or POSIX-style separators.
</>
(Fn [a, b] String)
(</> before after)
joins before
and after
using the default path separator.
absolute?
(Fn [(Ref String a)] Bool)
(absolute? p)
checks whether a path is absolute.
As such, it is the inverse to relative?.
add-extension
(Fn [(Ref String a), (Ref String b)] String)
(add-extension p ext)
adds an extension ext
to a path p
.
cwd
(Fn [] (Maybe String))
(cwd)
returns the current working directory as a Maybe
. The ways in
which it can fail are OS-dependent, but it should happen relatively rare.
drop-extension
(Fn [(Ref String a)] String)
(drop-extension p)
drops the extension of a path p
. Does nothing if there
is none.
extension
(Fn [(Ref String a)] (Maybe String))
(extension p)
gets the extension of a file as a Maybe
.
filename
(Fn [(Ref String a)] (Maybe String))
(filename p)
gets the filename of the path p
as a (Maybe String)
.
It will return Nothing
if an empty string is passed.
get-search-path
(Fn [] (Maybe (Array String)))
(get-search-path)
gets the PATH
environment variable and splits it.
has-extension?
(Fn [(Ref String a)] Bool)
(has-extension? p)
cheks whether the path p
has an extension.
is-extension?
(Fn [(Ref String a), (Ref String b)] Bool)
(is-extension? p ext)
checks whether the path p
has the extension ext
.
join
(Fn [(Ref (Array String) a)] String)
(join ps)
joins the path components ps
into a path.
As such, it is the inverse to split.
relative?
(Fn [(Ref String a)] Bool)
(relative? p)
checks whether a path is relative.
As such, it is the inverse to absolute?.
replace-extension
(Fn [(Ref String a), (Ref String b)] String)
(replace-extension p ext)
replaces the extension of a path p
with ext
. Adds
an extension if there previously was none.
search-path-separator
Char
is the separator for the PATH
environment
variable we use on this OS.
separator?
(Fn [(Ref Char StaticLifetime)] Bool)
(separator? c)
checks whether the character c
is a separator for the
PATH
environment variable on this OS.
split
(Fn [(Ref String a)] (Array String))
(split p)
splits the path p
into its components.
As such, it is the inverse to join.
split-extension
(Fn [(Ref String a)] (Maybe (Pair String String)))
(split-extension p)
splits the path p
on its extension.
It will return a (Maybe (Pair String String))
. Maybe
because there might not
be an extension, and Pair
because it will return the part before and after
the extension.
Examples on POSIX:
(split-extension "file.txt")
; => (Maybe.Just (Pair "file" "txt"))
(split-extension "file")
; => (Maybe.Nothing)
(split-extension "file/file.txt")
; => (Maybe.Just (Pair "file/file" "txt"))
(split-extension "file.txt/veit")
; => (Maybe.Nothing)
(split-extension "file.txt/veit.ext")
; => (Maybe.Just (Pair "file.txt/veit" "ext"))
(split-extension "file/path.txt.bob.fred")
; => (Maybe.Just (Pair "file/path.txt.bob" "fred"))
split-search-path
(Fn [(Ref String a)] (Array String))
(split-search-path p)
splits a PATH
environment variable p
.